MVC 2:VB.NET 2010 中的 Html.TextBoxFor 等

发布于 2024-08-31 10:53:06 字数 1795 浏览 8 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谁与争疯 2024-09-07 10:53:06

啊,我太蠢了;我的继承使用了 <>,仅适用于 C#;我需要:

System.Web.Mvc.ViewPage(Of MyModel)

这解决了错误。

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.

甜妞爱困 2024-09-07 10:53:06

您是否更改了 Page 指令中的语言:

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

Did you change the language in the Page directive:

<%@ Page 
    Language="VB" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<SomeModel>" %>
断肠人 2024-09-07 10:53:06

我不是VB专家,但我认为

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

应该改为这样

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

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>" %> 
栩栩如生 2024-09-07 10:53:06

编辑:更新;发现 TextBoxFor 查找 Expression(Of Func(Of Object, Object)) 并通过强制转换来执行此操作:

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

但现在我收到运行时错误:

描述:编译服务此请求所需的资源期间发生错误。请查看以下具体错误详细信息并适当修改您的源代码。

编译器错误消息: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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文