ASP.NET MVC 2 多个 PartialView 表单字段名冲突
我有一个 ASP.NET MVC 2 网页,在其中呈现多个相同类型的 PartialView。每个部分视图都包含一个用于发布的 Ajax 表单。该表单包括:
- 输入:EditorFor(m => m.content)
- 输入的验证:ValidationMessageFor(m => m.Content)
问题在于,因为页面上有超过 1 个这样的表单输入字段的名称存在冲突。我可以通过添加 EditorFor() 重载的 'htmlFieldName' 属性来解决这个问题,但这会导致另一个问题;验证不再起作用,因为输入字段的名称发生了变化......
有什么想法吗?
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 input: 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我解决了。
问题是冲突不是因为输入字段的 id,而是因为验证字段。现在它的工作原理如下:
其中 ValidationMessageId 是模型的只读字符串属性,它返回验证字段的唯一 html id。
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:
where ValidationMessageId is a readonly string property of the model which returns an unique html id for the validation field.