5

I have the following model class which contains a bool value:-

Public class Server
{
        public bool IsIPUnique { get; set; }
}

Currently i am using the following to display the check box inside my Razor view:-

<input type="CheckBox" name="IsIPUnique" value="true" @(Html.Raw(Model.IsIPUnique ? "checked=\"checked\"" : ""))/> IP Unique.

but i read about the EditorFor template, and it can automatically create the check box and check/uncheck it based on the model values so i tried the following :-

@Html.EditorFor(model=>model.IsIPUnique)<span>test</span>

so my question is if i can replace my old code with the new one that uses the EditorFor ?, or asp.net mvc might deal with these values differently ? Thanks

7
  • 1
    So what happened when you tried the new code? Commented Jun 30, 2014 at 17:01
  • basically it generates the same HTML markup , but i am not sure about using the EditorFor instead of checkbox ? Commented Jun 30, 2014 at 17:05
  • Have you considered using Html.CheckBoxFor? Commented Jun 30, 2014 at 17:06
  • EditorFor is great when you don't care what kinds of inputs mvc generates for your model (meaning no editor templatE) OR when you want specific control over how your model (or part of your model) is generated. For the most part however you fill find the html helper methods like CheckBoxFor will do the trick for you. Commented Jun 30, 2014 at 17:09
  • Also, the main thing to keep in mind when using your forms is that you must set the name property for proper model binding and validation and such. MVC takes care of this for you when you use the html helper methods. Commented Jun 30, 2014 at 17:09

1 Answer 1

6

Basically you have 3 possibilities:

Write HTML manually (as you have done)

I would avoid writing the HTML manually if there is a HTML helper available. Manually written HTML is prone to errors which can cause problems with model binding.

Use the specific HTML helpers (Html.CheckBoxFor)

The specific HTML helpers add a layer of abstraction to all controls. It's easy to modify the template of all controls that use the same HTML helper and it makes your views more readable.

Use the general EditorFor

The EditorFor HTML helper is great if your model datatypes change often. The EditorFor will adjust the input fields automatically to the new datatype and won't throw an error (as with the specific HTML helpers). It is also a bit harder to add HTML attributes to the EditorFor while the specific HTML helpers often have overloads for them. However, this is fixed in MVC 5.1: http://weblogs.asp.net/jongalloway/looking-at-asp-net-mvc-5-1-and-web-api-2-1-part-3-bootstrap-and-javascript-enhancements

Conclusion: in your case I would use the CheckBoxFor HTML helper because the datatype won't change likely and it will make the view cleaner

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

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.