MVC 2:VB.NET 2010 中的 Html.TextBoxFor 等
我有一个 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
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
啊,我太蠢了;我的继承使用了 <>,仅适用于 C#;我需要:
这解决了错误。
Ahh, dumb on my part; my inherits used <>, which is C# only; I needed:
And that fixed the error.
您是否更改了 Page 指令中的语言:
Did you change the language in the Page directive:
我不是VB专家,但我认为
应该改为这样
I am not a VB expert but I think this
should be changed to this
编辑:更新;发现 TextBoxFor 查找 Expression(Of Func(Of Object, Object)) 并通过强制转换来执行此操作:
但现在我收到运行时错误:
描述:编译服务此请求所需的资源期间发生错误。请查看以下具体错误详细信息并适当修改您的源代码。
编译器错误消息:BC30205:预期语句结束。
源错误:
第 46 行:_
第47行:公共类views_validation_index_aspx
第 48 行:继承 System.Web.Mvc.ViewPage
第 49 行:实现 System.Web.SessionState.IRequiresSessionState、System.Web.IHttpHandler
第 50 行:
源文件:C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\5a4e2626\dee8be7e\App_Web_index.aspx.b3b8acce.cmmfnwms.0.vb 行:48
我无法获胜使用 VB :-) 有什么想法吗?
EDIT: An update; found out that TextBoxFor looks for Expression(Of Func(Of Object, Object)) and by casting to do this it works:
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?