MVC 2:Html.TextBoxFor??2010?VB.NET
-
04-10-2019 - |
题
?????????C#ASP.NET MVC 2.0??,?????????????,??????:
<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>
?????VB.NET,???VB.NET 10??????,?:
<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>
??????????,????,?????,????????,???ASP.NET MVC 2.0,??????????????...??MVC 2????????????.NET 2010?
????VB.NET??,?????????VB:
“%>???Model????;??????????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
???
解决方案
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.
其他提示
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?