如何当一个模型包含多个元素美元asp.net MVC 3 P $ pvent HTML输入ID重复 [英] how to prevent html input id duplication on asp.net mvc 3 when a model contains multiple elements
本文介绍了如何当一个模型包含多个元素美元asp.net MVC 3 P $ pvent HTML输入ID重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一种观点认为,允许用户插入图示的多个相同类型的元素。
I have a view that allows the user to inset multiple elements of the same type.
@foreach (var gm in Model)
{
<tr><td>
@Html.TextBoxFor(model => gm.name)
</tr></td>
}
但是,当我看到生成的HTML被复制ID和姓名
But, when I saw the generated HTML it is duplicating the id and the name
<tr><td>
<input id="gm_name" name="gm.name" type="text" value="" />
</td></tr>
<tr><td>
<input id="gm_name" name="gm.name" type="text" value="" />
</td></tr>
有没有办法来自动添加1,2,3等和id的结尾,例如:
Is there a way to automatically add an 1,2,3,etc and the end of id, example:
<tr><td>
<input id="gm_name1" name="gm.name1" type="text" value="" />
</td></tr>
<tr><td>
<input id="gm_name2" name="gm.name2" type="text" value="" />
</td></tr>
我能做到这一点,如果我生成所有的HTML,但是,有一个自动的方式来实现这一目标?
I can do that if I generate all the html, but, is there an automatically way to achieve that?
感谢
推荐答案
而不是使用TextBoxFor,只需使用的文本框方法,你可以指定你想要使用的名称。像这样的东西应该工作,我想:
Rather than using TextBoxFor, just use the TextBox method, and you can specify the name you want it to use. Something like this should work, I think:
@foreach (int i = 0; i < Model.Count; i++)
{
var gm = Model[i];
<tr><td>
@Html.TextBox("gm.name" + i, gm.name, new {id = "gm_name" + i})
</tr></td>
}
这篇关于如何当一个模型包含多个元素美元asp.net MVC 3 P $ pvent HTML输入ID重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文