【发布时间】:2011-09-21 14:00:29
【问题描述】:
在将选择列表的数据源包含在视图模型中时,建议的最佳实践似乎存在脱节。例如,许多最佳实践博客文章都会推荐以下内容:
视图模型:
public class InvoiceViewModel
{
[UIHint("SelectInvoiceType")]
public int idInvoiceType { get; set; }
/* snip */
/* I'll use this in the view to generate a SelectList */
public List<invoicetype> InvoiceTypes { get; set; }
}
但是当我们进入编辑器模板时,Model 对象将只是 int,不知道包含的视图模型:
SelectInvoiceType.cshtml
@model int
@{
Layout = "~/Views/Shared/_EditorFormItem.cshtml";
List<SelectListItem> selList = /* nothing to say here, really */;
}
@section DataContent {
@Html.DropDownListFor(m => Model, selList, null)
}
所以,除非我遗漏了什么,否则这两个“最佳实践”——视图模型中的模板化视图助手和强类型列表数据——只是不能一起使用。您必须将列表数据填充到 ViewBag 中。听起来对吗?
很抱歉听起来难以置信,但我觉得我一定错过了什么。
【问题讨论】: