为@HtmlEditorFor设置"id"和"name"?

Mar*_*iah 11 asp.net-mvc-3

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

@Html.EditorFor(model => model.value, new { @id = "testid1", @name="testname1"})
Run Code Online (Sandbox Code Playgroud)

如何设置idname

Bui*_*ted 14

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

@Html.TextBoxFor(m => m.value, new { id = "testid1", name="Testname1" })
Run Code Online (Sandbox Code Playgroud)

您还可以为要为其创建编辑器的特定类型创建自定义编辑器模板.


veb*_*ebs 10

 @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 } })
Run Code Online (Sandbox Code Playgroud)

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


jmj*_*rri 5

EditorFor允许设置name和id属性(用.Net 4.6.1测试)@Html.EditorFor(m => m.value, null, "Testname1") 将生成<input class="text-box single-line" id="Testname1" name="Testname1" type="text" >