【发布时间】: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