【问题标题】:@Html.DropDownList set selected value dynamic MVC 4 Razor@Html.DropDownList 设置选定值动态 MVC 4 Razor
【发布时间】:2014-08-30 00:13:52
【问题描述】:

我使用我的系统创建@ViewBag Html.DropDownList。对于普通课程,它可以工作。但是我的模型中有一个 ICollection 我正在动态创建 @Html.DropDownList 但不知道如何设置项目的值并且不得不尝试出错:

DataBinding: 'System.Web.Mvc.SelectListItem' does not contain a property with the name 'Key'.

控制器

ViewBag.ListaCaracteristica = new SelectList(ListagemPadrao.ListaTipoCaracteristica(), "Key", "Texto");

模型项目LPesquisa

public class ItemLPesquisa
{
    public int Key { get; set; }
    public string Texto { get; set; }
}

列表

public static IEnumerable<ItemLPesquisa> ListaTipoCaracteristica()
{
    List<ItemLPesquisa> retorno = new List<ItemLPesquisa>();
    retorno.Add(new ItemLPesquisa { Key = 1, Texto = "Data" });
    retorno.Add(new ItemLPesquisa { Key = 2, Texto = "Texto Curto" });
    retorno.Add(new ItemLPesquisa { Key = 3, Texto = "Número Inteiro" });
    retorno.Add(new ItemLPesquisa { Key = 4, Texto = "Número Decimal" });
    return retorno;
}

查看

<div class="controls">
    <ul id="PhonesEditor" style="list-style-type: none">
    @if (Model.ListaProdutoCaracteristica != null)
    {
        foreach (var item in Model.ListaProdutoCaracteristica)
        {
            <div class="row">
                <input type="hidden" name="ListaProdutoCaracteristica[@linha.ToString()].ProdutoPadraoID" value="3">
                <input type="hidden" name="ListaProdutoCaracteristica[@linha.ToString()].ProdutoPadraoCaracteristicaID" value="3">

                <div class="col-md-3">
                    @Html.LabelFor(m => item.TipoCaracteristicaID) @Html.ValidationMessageFor(m => item.TipoCaracteristicaID)
                    @Html.DropDownList("item[" + @linha.ToString() + "].TipoCaracteristicaID", 
                        new SelectList((SelectList)ViewBag.ListaCaracteristica, "Key", "Texto", 2), String.Empty,
                        new { @class = "form-control" })
                </div>

                <div class="col-md-7">
                    @Html.LabelFor(m => m.Descricao) @Html.ValidationMessageFor(m => m.Descricao)
                    @Html.TextBoxFor(m => m.Descricao, new { @class = "form-control" })
                </div>                    
            </div>
            linha++;
        }
    }
    </ul>
</div>

此代码出错

@Html.DropDownList("item[" + @linha.ToString() + "].TipoCaracteristicaID", 
  new SelectList((SelectList)ViewBag.ListaCaracteristica, "Key", "Texto", 2), String.Empty,
  new { @class = "form-control" })

【问题讨论】:

    标签: c# asp.net asp.net-mvc


    【解决方案1】:

    ViewBag.ListaCaracteristica 已经是 SelectList,所以不要尝试再次创建它(SelectList 没有属性名称 Key,只有你的模型有)

    改变

    @Html.DropDownList("item[" + @linha.ToString() + "].TipoCaracteristicaID", 
      new SelectList((SelectList)ViewBag.ListaCaracteristica, "Key", "Texto", 2),
      String.Empty, new { @class = "form-control" })
    

    @Html.DropDownList("item[" + @linha.ToString() + "].TipoCaracteristicaID",
      (SelectList)ViewBag.ListaCaracteristica, 
      String.Empty, new { @class = "form-control" })
    

    编辑

    您使用帮助器命名控件的方式没有意义,它们不会绑定到任何东西(您没有名为 item 的属性)

    for (int i = 0; i < Model.ListaProdutoCaracteristica.Count; i++) {
      // Generate the hidden inputs this way
      @Html.HiddenFor(m => m.ListaProdutoCaracteristica[i].ProdutoPadraoID)
      // Generate the drop down this way
      @Html.DropDownListFor(m => m.ListaProdutoCaracteristica[i].TipoCaracteristicaID, (SelectList)ViewBag.ListaCaracteristica)
    }
    

    现在如果ListaProdutoCaracteristica[0].TipoCaracteristicaID is say 3, then the third item of yourSelectListwill be selected and displayNúmero Inteiro 的值`

    【讨论】:

    • @Html.DropDownList("item[" + @linha.ToString() + "].TipoCaracteristicaID", (SelectList)ViewBag.ListaCaracteristica, String.Empty, new { @class = "form-control" }) 未设置项目值
    • 我的回答是解决异常。您使用DropDownList 的方式真正可怕并创建&lt;select name="item[#].TipoCaracteristicaID" ..&gt;,其中#@linha 的值(无论是什么)。在任何情况下,您的模型中都没有名为 item 的集合,因此它不会绑定到任何东西。我会更新我的答案来解决这个问题。
    • @StephenMueche 谢谢你的回复。会以某种方式处理这个和对象列表吗?会以某种方式处理这个和对象列表吗?对于来自 Main 的 ListaProdutoCaracteristica 是 ICollection 模型
    猜你喜欢
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    相关资源
    最近更新 更多