views:

13

answers:

2

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>
A: 

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()))%>
Nathan Taylor
Nathan, do you mind looking at the second code sample I posted? The original way I wrote it works, with your fix, but there are no value="true" and value="false" in the <option> tags. I figured that using REAL SelectListItems would resolve this. Any ideas?
Blackcoil
@Blackcoil in HTML if there is no value attribute on an `<option />` element then the element's innerText is treated as the value.
Nathan Taylor
Got it, thanks!
Blackcoil
A: 

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.

jim