Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Html.EditorFor() Renders DropDownList Instead of CheckBox

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.

like image 700
Jonathan Wood Avatar asked Dec 12 '11 15:12

Jonathan Wood


People also ask

What are the advantages of the HTML EditorFor () method?

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.

How do I add an EditorFor style?

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.

What is HTML DropDownListFor?

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.

How to create Edit View in MVC?

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.


2 Answers

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".

like image 70
Paul Avatar answered Oct 17 '22 18:10

Paul


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" }) %>
<% } %>
like image 6
Alexander Yezutov Avatar answered Oct 17 '22 17:10

Alexander Yezutov