mo...">

Set "id" and "name" for @HtmlEditorFor?

I am trying to set id and name for @Html.EditorFor , but this does not work:

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

How to set id and name ?

+5
asp.net-mvc-3
Oct 23 '11 at 19:52
source share
3 answers

EditorFor does not allow adding htmlAttributes. For specific types of editors, you will need to use TextBoxFor (or any other type that you use).

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

You can also create a custom editor template for the specific type for which you are creating an editor.

+11
Oct 23 '11 at 20:13
source share

EditorFor allows you to set the name property and id (verified with .Net 4.6.1) @Html.EditorFor(m => m.value, null, "Testname1") will generate <input class="text-box single-line" id="Testname1" name="Testname1" type="text" >

+2
Jun 26 '17 at 17:42 on
source share
  @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 } }) 

Use insted of @name and add htmlAttributes to your code so that it works for me

0
Mar 29 '18 at 9:30
source share



All Articles