trying to make an MVC EditorTemplate for a Bool th

2019-09-01 15:45发布

问题:

I am making an MVC EditorTemplate for the bool data type that will render a drop down list with options True or False, AND set the selected value to the value of the Model.

The DDL renders but it's not being set to the value of the model.

What am I missing here?

Here is my view code:

<%:Html.EditorFor(model => model.Association.Active, "Bool")%>

Here is my template code:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<bool>" %>
<%
    string[] values = new string[2];
    values[0] = "True";
    values[1] = "False";
 %>
<div class="editor-row">
<div class="editor-label">
    <%= Html.LabelFor(model => model)%>
</div>
<div class="editor-field">
    <%= Html.DropDownListFor(model => model, new SelectList(values, Model))%>
    <%= Html.ValidationMessageFor(model => model) %>
</div>
</div>

I embellished the code a bit so that I am using REAL SelectListItems instead of strings. But this still doesn't work. Now I get a DDL of two strings that say "System.Web.MVC.SelectListItem" instead of "true" and "false". Why does that happen here?

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<bool>" %>
<%
    SelectListItem item1 = new SelectListItem();
    item1.Text = "True";
    item1.Value = "True";

    SelectListItem item2 = new SelectListItem();
    item2.Text = "False";
    item2.Value = "False";

    if (Model == true)
    {
        item1.Selected = true;
        item2.Selected = false;
    }
    else
    {
        item1.Selected = false;
        item2.Selected = true;
    }

    List<SelectListItem> items = new List<SelectListItem>();
    items.Add(item1);
    items.Add(item2);
 %>
<div class="editor-row">
<div class="editor-label">
    <%= Html.LabelFor(model => model)%>
</div>
<div class="editor-field">
    <%= Html.DropDownListFor(model => model, new SelectList(items))%>
    <%= Html.ValidationMessageFor(model => model) %>
</div>
</div>

回答1:

The problem is that SelectList's selectedValue constructor parameter is looking for an exact instance of object selectedValue in the value collection passed to it. In your case, you are passing a string[] as the value set, and a Boolean as the selected object and since (bool)true != (string)"True" no matching item is found.

Instead do this:

<%= Html.DropDownListFor(model => model, new SelectList(values, Model.ToString()))%>


回答2:

So finally code worked:

public List<SelectListItem> YesNo()         
{

         SelectListItem item1 = new SelectListItem();
         item1.Text = "Yes";
         item1.Value = "True";
         SelectListItem item2 = new SelectListItem();
         item2.Text = "No";
         item2.Value = "False";
         List<SelectListItem> items = new List<SelectListItem>();
         items.Add(item1);
         items.Add(item2);
         return items;
}

In View:

 @Html.DropDownListFor(model => model.CustomerAccess,new SelectList(
                        new OrderProgression.Model.CommonList().YesNo(),
                        "Value","Text",Model.CustomerAccess.ToString()))


回答3:

Blackcoil - have you tried tweaking the 1st line to:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.Boolean?>" %>

probably won't help, but this is how my EditorTemplate for a custom checkbox is defined as i expect nullable types.