14

I'm trying to set id and name for @Html.EditorFor, but this isn't working:

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

How do I set id and name?

GSerg
  • 76,472
  • 17
  • 159
  • 346
Mariah
  • 727
  • 5
  • 12
  • 25

3 Answers3

17

EditorFor does not allow for adding htmlAttributes. For the specific types of editors you'll have to use TextBoxFor (or whatever type you're using).

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

You can also create a custom editor template for the particular type you're creating an editor for.

Buildstarted
  • 26,529
  • 10
  • 84
  • 95
15
 @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 Name insted of @name and add htmlAttributes in your code its working for me

vebs
  • 325
  • 1
  • 4
  • 11
7

EditorFor allows to set name and id property (tested 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" >

jaymjarri
  • 3,048
  • 1
  • 18
  • 7