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
?
Just add the id property to the html-attributes. That will override the default id generated by the editorfor-helper-methode.
By default, the TextBoxFor element will have an ID and NAME property that matches the expression property of the element. If you want to specify an ID or NAME that's different from the expression property, you can use the htmlAttributes overload param.
The Html. Editor() or Html. EditorFor() extension methods generate HTML elements based on the data type of the model object's property.
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" >
@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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With