MVC 2: Html.TextBoxFor, etc. in VB.NET 2010


MVC 2: Html.TextBoxFor, etc. in VB.NET 2010



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:

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

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.




Why jquery ajax calls fails after session timeout in asp.net mvc?

1:



ASP.NET or ASP.NET MVC or Silverlight or a mixture?
Ahh, dumb on my part; my inherits used <>, which is C# only; I needed:.
Why are my aspx pages not being cached on the client?
System.Web.Mvc.ViewPage(Of MyModel) 
And that fixed the error..
Multible jquery .load() methods not running concurrently in MVC 2


How 404 error asp.net mvc 2 IIS 7?


ASP.NET MVC Error: Page is not allowed here because it does not extend class 'System.Web.Page'

2:



Extension method not working if I set controller property in Action, works in OnExecuting
Did you change the language in the Page directive:.
Using external javascript files with asp.net MVC
<%@ Page      Language="VB"      MasterPageFile="~/Views/Shared/Site.Master"      Inherits="System.Web.Mvc.ViewPage<SomeModel>" %> 


3:


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>" %>  


4:


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?.



68 out of 100 based on 33 user ratings 58 reviews

@