b3r3ch1t
b3r3ch1t

Reputation: 165

Attribute css in @Html.EditorFor

I have a textbox where I setup the class attibute css rounded.

I use this code:

 @Html.TextBoxFor(m => m.level, new { @class = "form-control rounded", @placeholder = "Level" })

When I run the browser renderizer this as:

 <input class="form-control rounded"  id="level" name="level" placeholder="Level" type="text" " />

If I change to:

@Html.EditorFor(m => m.level, new { @class = "form-control rounded", @placeholder = "Level" })

The browser rederizer as:

 <input class="text-box single-line"  id="level" name="level" placeholder="Level" />

There are a way to use rounded with @Html.EditorFor?

What is the benefit to use @Html.EditorFor?

Upvotes: 0

Views: 685

Answers (2)

Grizzly
Grizzly

Reputation: 5943

I think this is what you are looking for:

@Html.EditorFor(m => m.level, new { htmlAttributes = new { @class = "form-control rounded", @placeholder = "Level" }})

Upvotes: 0

Shashank Sood
Shashank Sood

Reputation: 480

TextBoxFor: It will render like text input html element corresponding to specified expression. In simple word it will always render like an input textbox irrespective datatype of the property which is getting bind with the control.

EditorFor: This control is bit smart. It renders HTML markup based on the datatype of the property. E.g. suppose there is a boolean property in model. To render this property in the view as a checkbox either we can use CheckBoxFor or EditorFor. Both will be generate the same markup.

What is the advantage of using EditorFor?

As we know, depending on the datatype of the property it generates the html markup. So suppose tomorrow if we change the datatype of property in the model, no need to change anything in the view. EditorFor control will change the html markup automatically.

Upvotes: 1

Related Questions