I try add html attribute for EditorFor
@Html.EditorFor(model => model.UserName, new { style = "width: 100px" })
But any way I get
<input id="UserName" class="text-box single-line" type="text" value="" name="UserName">
I try add html attribute for EditorFor
@Html.EditorFor(model => model.UserName, new { style = "width: 100px" })
But any way I get
<input id="UserName" class="text-box single-line" type="text" value="" name="UserName">
You can create a style class in .css like
Style can be appilied with html class attribute.
Instead of using
@Html.EditorFor(model => model.UserId)
Use
@Html.TextBoxFor(model => model.UserId, new { @Value = "Prabhat" })
If you need to use EditorFor and not TextBoxFor and have multiple editors on the page, but only need to change the width of specific ones... The easiest what is to add a css style to the class ID as such:
For what it's worth I had this same problem. I resolved it by using a slightly different version of Narenda V's solution.
I created a class like this: (note - min-width instead of width)
then in my view:
When I did not use min-width, but width, it did not work. Bob
EditorFor is going to overwrite those attributes. See Html attributes for EditorFor() in ASP.NET MVC for workarounds.
If all you need to change is the display of the width, and you're not relying on any Editor Templates then the easiest fix is to use TextBoxFor instead
I had the same issue and this solved it...