Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 249
Mariah Avatar asked Oct 23 '11 19:10

Mariah


People also ask

How do I change my razor view ID?

Just add the id property to the html-attributes. That will override the default id generated by the editorfor-helper-methode.

How can we give ID to textbox in HTML?

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.

What is the use of HTML EditorFor?

The Html. Editor() or Html. EditorFor() extension methods generate HTML elements based on the data type of the model object's property.


2 Answers

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" >

like image 128
jaymjarri Avatar answered Sep 28 '22 18:09

jaymjarri


 @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

like image 36
vebs Avatar answered Sep 28 '22 20:09

vebs