How can I set id using Html.EditorFor with MVC3
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
@Html.EditorFor(x => x.Order, null, string.Format("Order_{0}", Model.Row))
have you tried creating an editor template for your x.Order
?
have this on your editor template:
<input type="text" id="@ViewData["id"]">
and use this on your view page:
@Html.EditorFor(x => x.Order, new { id = string.Format("Order_{0}", Model.Row) })
I know this question is pretty old, but all I needed to do was the following:
@Html.EditorFor(modelItem => item.EndDate,
new { htmlAttributes = new { @class = "form-control", @id = @endID } })
You can give it whatever classes or id that you want/need. I have this earlier in my page as well to create a different ID for each item:
string endID = "end-" + @item.ID;
Hopefully this helps someone in the future.