0

Here is my MVC View page VB code, that ViewData("Maximum exceed") is passed from controller. That is, if ViewData("Maximum exceed") is true, it will render a row with label control. Is there anything wrong with my VB syntax? How can I put VB condition in the View page?

@Using (Html.BeginForm())
@<table>  

     @If ViewData("IsMaximumExceed") Then
     <tr>
         <td colspan="3">
             @Html.Label(
                 Sub(settings)
                     settings.AssociatedControlName = "MaximumExceed"
                     settings.Text = "Maximum Exceed"                                                               
                 End Sub).GetHtml()
         </td>
     <tr>
</table>
End Using
5
  • 1
    What is the problem? It looks okay to me... though note that VB is not supported by ASP.NET Core (including Razor) so you should transition to C# as soon as possible. Commented Apr 13, 2017 at 0:31
  • Note that in ASP.NET MVC there are no "Controls" - the Html.Label is a "HTML Helper" which renders HTML directly in a stateless manner. Commented Apr 13, 2017 at 0:32
  • I got VB syntax error on <tr><td> element in razor view, so what's wrong is the syntax? Commented Apr 13, 2017 at 0:51
  • I have modified the code above, wondering what's wrong with VB syntax. Commented Apr 13, 2017 at 1:35
  • Well, you're missing an End If, for starters. Commented Apr 13, 2017 at 4:06

2 Answers 2

1

You have the arguments to Label(HtmlHelper, String, String) the wrong way around. The first string parameter is the expression, the second string parameter is the labelText.

Your If ViewData("Maximum exceed") checks to see if there's a dictionary member with key "Maximum exceed" but the expression value is "MaximumExceed" - they don't match.

That said, you should prefer a strongly-typed "view model" object instead of using the untyped ViewData or ViewBag objects.

Public Class MyPageViewModel

    Public MaximumExceeded As Boolean

End Class

...

Public Function MyControllerAction() As ActionResult 

    Dim viewModel As New MyPageViewModel
    viewModel.MaximumExceeded = True
    Return Me.View( viewModel )

End Sub

...

@Model MyPageViewModel

@If Model.MaximumExceeded Then

    @Html.Label( NameOf(Model.MaximumExceeded), "Maximum exceeded" )

End If

Note my use of the NameOf operator to get the string name of the property. Or better yet, use Html.LabelFor.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks very much for your reply. I will apply the expression in the razor view.
However I would like to display Label control in a table row, how can I put VB syntax condition in the view?
<table> @If ViewData("Maximum exceed") Then <tr> <td colspan="3"> @Html.Label( NameOf(Model.MaximumExceeded), "Maximum exceed") </td> </tr> End If
I have modified the code above, wondering what's wrong with VB syntax.
1

Since @Using (Html.BeginForm()) statement begins a form in "code block mode", in VB.NET context you need to set "text mode" either using @: (single line operator), @<text>...</text> (single or multiple line mode) or simply @ followed with HTML block element:

' Using @:
@Using (Html.BeginForm())
@:<table cellpadding="0" cellspacing="0" style="width: 800px; height:100px;">
    <!-- inner table -->
@:</table>
End Using

' Using @<text>...</text>
@Using (Html.BeginForm())
@<text><table cellpadding="0" cellspacing="0" style="width: 800px; height:100px;">
       <!-- inner table -->
 </table></text>
End Using

'Using @<tag>...</tag>
@Using (Html.BeginForm())
    @<table cellpadding="0" cellspacing="0" style="width: 800px; height:100px;">
           <!-- inner table -->
    </table>
End Using

The reason behind usage of operators to enclose HTML tags is that VB.NET allows inline XML elements by applying "text mode" inside "code block mode" which C# doesn't have that feature (see Razor syntax for VB.NET).

Hence, the provided sample in question should be modified like this, using @ before <tr> tag to create HTML block element under If code block:

@Using (Html.BeginForm())
@<table cellpadding="0" cellspacing="0" style="width: 800px; height:100px;">  
 @If ViewData("IsGroupVessel") Then
 @<tr>
      <td colspan="3">
      @Html.DevExpress().Label(Sub(settings)
            settings.AssociatedControlName = "GroupBooking"
            settings.Text = "Maximum Exceeded"                                                               
       End Sub).GetHtml()
      </td>
  </tr>
  End If
 </table>
End Using

As @Dai stated in his answer, I also prefers usage of a strongly-typed "view model" with properties instead passing ViewBag or ViewData, hence the modified code is shown below:

Model

Public Class ViewModel
   Public Property IsExceeded As Boolean
   ' other models
End Class

Controller

Public Function ActionMethod() As ActionResult
   Dim model As New ViewModel
   ' other logic
   model.IsExceeded = True
   ' other logic
   Return Me.View(model)
End Function

View

@ModelType ViewModel

@Using (Html.BeginForm())
@<table cellpadding="0" cellspacing="0" style="width: 800px; height:100px;">
 @If Model.IsExceeded Then
 @<tr>
      <td colspan="3">
      @Html.DevExpress().Label(Sub(settings)
          settings.AssociatedControlName = "GroupBooking"
          settings.Text = "Maximum Exceeded"                                                               
      End Sub).GetHtml()
      </td>
  </tr>
  End If
  </table>
End Using

Related issues:

Razor View Engine Quirks in VB.NET

Using in Razor VB.net MVC not work as expected

2 Comments

I got it, this is exactly what I was looking for. Thanks so much.
@Julia Welcome to Stack Overflow. Please consider accepting an answer which solved your problem by clicking the check mark besides that answer, indicating that you've already found the solution. There is no obligation to do so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.