trying to make an MVC EditorTemplate for a Bool that renders a DDL with True or False

穿精又带淫゛_ 提交于 2020-01-25 09:53:04

问题


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.



来源:https://stackoverflow.com/questions/4036889/trying-to-make-an-mvc-editortemplate-for-a-bool-that-renders-a-ddl-with-true-or

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!