Asp.Net MVC2 Clientside Validation and duplicate I

2019-04-15 22:10发布

I'm using MVC2 with VS2010

I have a view that has two partial views in it: "Login" and "Register"

both partial views contains the Email address field i use the following in both partial views:

<%: Html.TextBoxFor(model => model.EmailAddress ) %><br /><%: Html.ValidationMessageFor(model => model.EmailAddress) %>

if i use both partial views on one page, it ends up having duplicate id's so validation happens across both views (even thopugh they are in seperate forms)

how can i go about eliminating this

1条回答
放我归山
2楼-- · 2019-04-15 22:59

For some controls you can specify the HTML attributes in an overload like this:

<%: Html.TextBoxFor(model => model.EmailAddress, new { id = 'my-unique-id" }) %>
<br />
<%: Html.ValidationMessageFor(model => model.EmailAddress, new { id = 'my-unique-id" }) %>

You can also either write your HTML by hand or use the older HTML helpers so you can add your own ID that way (you need to do this for Html.LabelFor() helpers)

<%: Html.TextBox( "EmailAddress", Model.EmailAddress, new { id = 'my-unique-id" } ) %>
查看更多
登录 后发表回答