Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set id using Html.EditorFor with MVC3

I am trying to set a field id as follows:

@Html.EditorFor(x => x.Order, new { id = string.Format("Order_{0}", Model.Row) }) 

but this results in the following and it seems my id is not being set:

<input type="text" value="334" name="item.Order" id="item_Order" class="text-box single-line"> 

Does anyone have an idea what I am doing wrong. I checked the allowable formats for EditorFor and looked on google for examples but I could not see anything that matched what I need.

like image 405
Samantha J T Star Avatar asked Feb 08 '12 06:02

Samantha J T Star


People also ask

How do I add ID to my razor?

Just add the id property to the html-attributes. That will override the default id generated by the editorfor-helper-methode. Show activity on this post. It is the same with @Html.

What does HTML EditorFor do?

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

How do I add an EditorFor style?

EditorFor does not allow for styling as there are no parameters for additional attributes. The reason for this is because the EditorFor doesn't always generate a single element as it can be overridden. To style a specific type of element you need to use the specific editor you want to use.


1 Answers

You should change to

@Html.TextBoxFor(x => x.Order, new { id = string.Format("Order_{0}", Model.Row) }) 

The second parameter of @Html.EditorFor is for view data, not for html attributes

like image 154
dohaivu Avatar answered Oct 13 '22 11:10

dohaivu