I'm new from WebForms to MVC 3 and have an issue with the @Html.EditorFor()
helper method.
I have a strongly typed view that represents data from a database, and one of the methods is of type bool?
. I'd like this to appear as a checkbox, but instead it appears as a dropdownlist with the options "Not Set", "True" and "False".
What is the simplest way to covert this to a regular checkbox?
I understand that I could change the data type to a plain old bool
, but this is a large EF entity I'm using and it seems a pain to have to recreate the entire class just for this. I also realize I'll lose the ability to track the "not set" state, but showing a simple checkbox is more important to me.
The advantages of EditorFor is that your code is not tied to an <input type="text" . So if you decide to change something to the aspect of how your textboxes are rendered like wrapping them in a div you could simply write a custom editor template ( ~/Views/Shared/EditorTemplates/string.
EditorFor does not allow for styling as there are no parameters for additional attributes. The reason for this is because the EditorFor doesn't always generate a single element as it can be overridden. To style a specific type of element you need to use the specific editor you want to use.
DropDownListFor() The Html. DropDownListFor<TModel,TProperty> extension method is a strongly typed extension method generates <select> element for the property specified using a lambda expression.
To create Edit view, right-click in the Edit() action method and click on Add View... It will open Add View dialogue, as shown below. In the Add View dialogue, keep the view name as Edit . Select Edit Template and Student Model class from dropdown, as shown below.
Use the checkbox helper method instead, @Html.CheckBoxFor()
It's rendering a drop down list as a check box wouldn't be able to provide the value "not set".
Basically, ASP.NET MVC has some default templates (you can read that here).
If you wish, you could add your own EditorTemplate and ASP.NET MVC will use it instead of default. For this you should place a file 'Boolean.{your-view-engine-extension}' (ex.: 'Boolean.aspx') into either ~/Views/ControllerName/EditorTemplates/ or ~/Views/Shared/EditorTemplates/ and override it with your own functionality.
Here is the default editor for Boolean, which can be enhanced by you:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<script runat="server">
private List<SelectListItem> TriStateValues {
get {
return new List<SelectListItem> {
new SelectListItem { Text = "Not Set", Value = String.Empty, Selected = !Value.HasValue },
new SelectListItem { Text = "True", Value = "true", Selected = Value.HasValue && Value.Value },
new SelectListItem { Text = "False", Value = "false", Selected = Value.HasValue && !Value.Value },
};
}
}
private bool? Value {
get {
if (ViewData.Model == null) {
return null;
}
return Convert.ToBoolean(ViewData.Model, System.Globalization.CultureInfo.InvariantCulture);
}
}
</script>
<% if (ViewData.ModelMetadata.IsNullableValueType) { %>
<%= Html.DropDownList("", TriStateValues, new { @class = "list-box tri-state" }) %>
<% } else { %>
<%= Html.CheckBox("", Value ?? false, new { @class = "check-box" }) %>
<% } %>
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