在我看来,我有以下几点:
<div class="editor-field">
@Html.DropDownListFor(x => x.Subject, (IEnumerable<SelectListItem>) ViewBag.Clients)
</div>
这就是我调用视图的方式:
var list = new List<SelectListItem>
{
new SelectListItem {Text = "Gas Order", Value = "delivery"},
new SelectListItem {Text = "Account Services", Value = "account"},
new SelectListItem {Text = "Service Call", Value = "service"},
new SelectListItem {Text = "Inquiry", Value = "inquiry"}
};
ViewBag.Clients = list;
返回视图();
这是在我的模型中:
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Text)]
[Display(Name = "Subject")]
public string Subject { get; set; }
我正在尝试返回所选值。
由于屏幕上的错误而重新加载屏幕时,我收到此错误:
具有键“Subject”的 ViewData 项属于“System.String”类型,但必须属于“IEnumerable”类型。
我假设这是由于这条线:
public string Subject { get; set; }
如果是这种情况,我如何将所选项目恢复到我的模型中?