I need to add css class and id in @Html.TextBox mvc4 at the same time. I try
@Html.TextBox("name", new { id = "name"}, new { @class = "text-field" })
but as result I get
<input class="text-field" id="name" name="name" type="text" value="{ id = name }">
I dont need attribute value here.
I need to get
<input type="text" value="" name="name" id="name" class="text-field" />
Correct overload method
public static MvcHtmlString TextBox(
this HtmlHelper htmlHelper,
string name,
Object value,
Object htmlAttributes
)
you want to use id
as HtmlAttribute, so you should use it in HtmlAttributes object. Correct usage of TextBox
is:
@Html.TextBox("name",
null,
new {
id = "name",
@class = "text-field"
})
if you place id
in route object, then id will be a route value.
Try
@Html.TextBox("name",
null,
new {
id = "name",
@class = "text-field"
})
I simply use the new keyword. For example see the following code
@Html.TextBoxFor(m => m.Venue, new { @class = "form-control", @id = "AnyCustomID" })
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