0

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?

1
  • EditorFor is for when you create a EditorTemplate and you want to reuse that partial view instead of using the default editor for the property type Commented Jun 24, 2016 at 18:45

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

I think this is what you are looking for:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.