试图让一个MVC EditorTemplate的呈现与真或假DDL一个布尔 [英] trying to make an MVC EditorTemplate for a Bool that renders a DDL with True or False

查看:83
本文介绍了试图让一个MVC EditorTemplate的呈现与真或假DDL一个布尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个MVC EditorTemplate为布尔数据类型将呈现一个下拉的选项真或假名单,并选定值设置为模型的价值。

的DDL呈现,但它不是被设置到该模型的值

我是什么在这里失踪?

下面是我的看法code:

 <%:Html.EditorFor(型号=> model.Association.Active,布尔)%>

下面是我的模板code:

 <%@控制语言=C#继承=System.Web.Mvc.ViewUserControl<布尔>中%GT;
<%
    字符串[] =值新的字符串[2];
    值[0] =真;
    值[1] =假;
 %GT;
< D​​IV CLASS =主编行>
< D​​IV CLASS =编辑标记>
    <%= Html.LabelFor(型号=>模型)%GT;
< / DIV>
< D​​IV CLASS =主编场>
    <%= Html.DropDownListFor(型号=>的模式,新的SelectList(值模型))%GT;
    <%= Html.ValidationMessageFor(型号=>模型)%GT;
< / DIV>
< / DIV>

我点缀code一下,让我使用REAL SelectListItems而不是字符串。但是,这仍然无法正常工作。现在,我得到两个字符串的DDL的说:System.Web.MVC.SelectListItem而不是真与假。为什么会在这里出现呢?

 <%@控制语言=C#继承=System.Web.Mvc.ViewUserControl<布尔>中%GT;
<%
    SelectListItem ITEM1 =新SelectListItem();
    item1.Text =TRUE;
    item1.Value =TRUE;    SelectListItem ITEM2 =新SelectListItem();
    item2.Text =假;
    item2.Value =假;    如果(型号==真)
    {
        item1.Selected = TRUE;
        item2.Selected = FALSE;
    }
    其他
    {
        item1.Selected = FALSE;
        item2.Selected = TRUE;
    }    清单< SelectListItem>项目=新的List< SelectListItem>();
    items.Add(ITEM1);
    items.Add(ITEM2);
 %GT;
< D​​IV CLASS =主编行>
< D​​IV CLASS =编辑标记>
    <%= Html.LabelFor(型号=>模型)%GT;
< / DIV>
< D​​IV CLASS =主编场>
    <%= Html.DropDownListFor(型号=>的模式,新的SelectList(项目))%GT;
    <%= Html.ValidationMessageFor(型号=>模型)%GT;
< / DIV>
< / DIV>


解决方案

的问题是的SelectList的了selectedValue构造函数参数的值,寻找对象了selectedValue 的确切实例收集传递给它。在你的情况,你通过一个的String [] 作为值集和一个布尔作为选择的对象,因为(布尔)真!=(字符串)真没有匹配的项目被发现。

相反做到这一点:

 <%= Html.DropDownListFor(型号=>的模式,新的SelectList(值,Model.ToString()))%GT;

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>

解决方案

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()))%>

这篇关于试图让一个MVC EditorTemplate的呈现与真或假DDL一个布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆