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>