使用MVC 5并且似乎无法将下拉列表绑定到我的模型。 DisplayNameFor正确绑定。这是我的观点。
<div class="row">
<h2>@ViewBag.Title</h2>
<hr />
<div class="col-lg-12" id="div1">
<form class="form-horizontal" role="form" method="post" action="@Url.Action("GetKeys", "Encryption")">
<div class="form-group">
@Html.DisplayNameFor(model => model.CarrierID)
<div class="col-sm-9">
@Html.DropDownListFor(model => model.CarrierID, new SelectList(ViewBag.Carriers, "id", "Name"), "----Select a Carrier----")
@Html.ValidationMessageFor(model => model.CarrierID)
</div>
</div>
</form>
</div>
</div>
模型看起来像这样
[Key]
public System.Guid EncryptionID { get; set; }
[Required]
[Display(Name = "Carrier Name:")]
public string CarrierID { get; set; }
public IEnumerable<SelectListItem> CarrierNames { get; set; }
[Required]
[Display(Name = "Text to Encrypt:")]
public string txtText { get; set; }
public string Thumbprint { get; set; }
Intelisense显示该模型不包含CarrierID的定义,想法?
答案 0 :(得分:0)
我刚刚测试了你的代码,并且该属性在智能感知中显示出来对我来说VS2013溢价很高。
也许这是你VS的问题?
值得一提的是我测试了表单帖子,可以看到更改viewbag选择列表时从下拉列表中传来的值:
@Html.DropDownListFor(model => model.CarrierID, MyModel.MySelectListItems())
public static IList<SelectListItem> MySelectListItems()
{
return new List<SelectListItem>{
new SelectListItem {Text = "option1", Value = "1"},
new SelectListItem{Text = "option2", Value = "2"}
};
}
答案 1 :(得分:0)
所以,我找到了解决方案。这是元素被引用的方式。
@Html.DropDownListFor(model => model.ElementAt<Professional_Services_Tools.Models.t_ConfigBasedEncryption>(0).CarrierID, new SelectList(ViewBag.Carriers, "Value", "Text"), "----Select a Carrier----")
@Html.ValidationMessageFor(model => model.ElementAt<Professional_Services_Tools.Models.t_ConfigBasedEncryption>(0).CarrierID)