为@HtmlEditorFor设置“id”和“name”?

时间:2011-10-23 19:52:49

标签: asp.net-mvc-3

我正在尝试为id设置name@Html.EditorFor,但这不起作用:

@Html.EditorFor(model => model.value, new { @id = "testid1", @name="testname1"})

如何设置idname

3 个答案:

答案 0 :(得分:12)

EditorFor不允许添加htmlAttributes。对于特定类型的编辑器,您必须使用TextBoxFor(或您正在使用的任何类型)。

@Html.TextBoxFor(m => m.value, new { id = "testid1", name="Testname1" })

您还可以为要为其创建编辑器的特定类型创建custom editor template

答案 1 :(得分:7)

 @Html.EditorFor(model => model.Beds[i].BedName, new { htmlAttributes = new { @class = "form-control", Name = "BedName" + Model.Beds[i].bedId, @id = "BedName" + Model.Beds[i].bedId } })

使用@name的名称,并在代码中添加htmlAttributes,为我工作

答案 2 :(得分:5)

EditorFor允许设置name和id属性(使用.Net 4.6.1测试)string[] matches = new string[]{}; 将生成matches [b] = hometeam.tostring() + " : " + awayteam.tostring() + homescore.tostring() + " : " + awayscore.tostring();

相关问题