开发者

ASP.NET MVC 2 Multiple PartialView Forms fieldname conflict

开发者 https://www.devze.com 2023-01-07 07:23 出处:网络
I have a ASP.NET MVC 2 webpage in which I render multiple PartialViews of the same type. Each partial view contains a Ajax-form for posting smth. The form consists of:

I have a ASP.NET MVC 2 webpage in which I render multiple PartialViews of the same type. Each partial view contains a Ajax-form for posting smth. The form consists of:

  • an input: EditorFor(m => m.content)
  • the validation for the i开发者_开发百科nput: ValidationMessageFor(m => m.Content)

The problem is that because there are more than 1 of these forms on the page there is a conflict in the names of the input fields. This I can solve by adding the 'htmlFieldName' property of the EditorFor() overload, but it causes another problem; the validation doesn't work anymore because the name of the input field changed...

Any ideas?


Ok, I solved it. The thing is that the conflict wasn't because of the ids of the input field, but of the validation field. So now it works like this:

<%= Html.EditorFor(m => m.Content)%>
<%= Html.ValidationMessageFor(
         m => m.Content, 
         null, 
         new { id = Model.ValidationMessageId, name = Model.ValidationMessageId })%>

where ValidationMessageId is a readonly string property of the model which returns an unique html id for the validation field.

0

精彩评论

暂无评论...
验证码 换一张
取 消