Question

I have this sample ASP.NET MVC 2.0 view in C#, bound to a strongly typed model that has a first name, last name, and email:

<div>
    First: <%= Html.TextBoxFor(i => i.FirstName) %>
    <%= Html.ValidationMessageFor(i => i.FirstName, "*") %>
</div>
<div>
    Last: <%= Html.TextBoxFor(i => i.LastName) %>
    <%= Html.ValidationMessageFor(i => i.LastName, "*")%>
</div>
<div>
    Email: <%= Html.TextBoxFor(i => i.Email) %>
    <%= Html.ValidationMessageFor(i => i.Email, "*")%>
</div>

I converted it to VB.NET, seeing the appropriate constructs in VB.NET 10, as:

<div>
    First: <%= Html.TextBoxFor(Function(i) i.FirstName) %>
    <%= Html.ValidationMessageFor(Function(i) i.FirstName, "*") %>
</div>
<div>
    Last: <%= Html.TextBoxFor(Function(i) i.LastName)%>
    <%= Html.ValidationMessageFor(Function(i) i.LastName, "*")%>
</div>
<div>
    Email: <%= Html.TextBoxFor(Function(i) i.Email)%>
    <%= Html.ValidationMessageFor(Function(i) i.Email, "*")%>
</div>

No luck. Is this right, and if not, what syntax do I need to use? Again, I'm using ASP.NET MVC 2.0, this is a view bound to a strongly typed model... does MVC 2 still not support the new language constructs in .NET 2010?

It's a VB.NET project and I correctly reference VB with this header:

" %>

Here is the definition of the Model class; the default project namespace is MvcSample.VB:

Namespace Models.Validation
    Public Class ValidationSampleTestClass
        <Required(ErrorMessage:="First name required.")> _
        Public Property FirstName() As String
            Get
                Return m_FirstName
            End Get
            Set(ByVal value As String)
                m_FirstName = value
            End Set
        End Property
        Private m_FirstName As String
        .
        .
        .
    End Class
End Namespace

Thanks.

Was it helpful?

Solution

Ahh, dumb on my part; my inherits used <>, which is C# only; I needed:

System.Web.Mvc.ViewPage(Of MyModel)

And that fixed the error.

OTHER TIPS

Did you change the language in the Page directive:

<%@ Page 
    Language="VB" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<SomeModel>" %>

I am not a VB expert but I think this

    <%@ Page 
Title="" 
Language="VB" 
MasterPageFile="~/Views/Shared/Site.Master" 
Inherits="System.Web.Mvc.ViewPage" %> 

should be changed to this

    <%@ Page 
Title="" 
Language="VB" 
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<Models.Validation.ValidationSampleTestClass>" %> 

EDIT: An update; found out that TextBoxFor looks for Expression(Of Func(Of Object, Object)) and by casting to do this it works:

<div>
    First: <%= Html.TextBoxFor(Function(i) DirectCast(i, ValidationSampleTestClass).FirstName)%>
    <%= Html.ValidationMessageFor(Function(i) DirectCast(i, ValidationSampleTestClass).FirstName, "*")%>
</div>
<div>
    Last: <%= Html.TextBoxFor(Function(i) DirectCast(i, ValidationSampleTestClass).LastName)%>
    <%= Html.ValidationMessageFor(Function(i) DirectCast(i, ValidationSampleTestClass).LastName, "*")%>
</div>
<div>
    Email: <%= Html.TextBoxFor(Function(i) DirectCast(i, ValidationSampleTestClass).Email)%>
    <%= Html.ValidationMessageFor(Function(i) DirectCast(i, ValidationSampleTestClass).Email, "*")%>
</div>

But now I get a runtime error:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30205: End of statement expected.

Source Error:

Line 46: _ Line 47: Public Class views_validation_index_aspx Line 48: Inherits System.Web.Mvc.ViewPage Line 49: Implements System.Web.SessionState.IRequiresSessionState, System.Web.IHttpHandler Line 50:

Source File: C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\5a4e2626\dee8be7e\App_Web_index.aspx.b3b8acce.cmmfnwms.0.vb Line: 48

I cannot win with VB :-) any ideas?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top