ASP MVC 4,在自定义编辑器模板中传递数据

时间:2013-07-24 07:35:50

标签: c# razor html-helper

我正在使用这行代码来调用自定义编辑器模板。

@Html.Editor("country", "CustomDropdown", CountrySelectionList)

我现在想在CustomDropdown视图中访问CountrySelectionList并迭代其项目:我该怎么做? CountrySelectionList的类型为SelectList

这是我在视图中尝试做的事情:

@model SelectList

@foreach (var item in Model.Items)
{
    <div>@item.ToString()</div>
}

但是出现Object reference not set to an instance of an object错误。

那我怎么能这样做?我想做我自己的下拉列表:如果不迭代SelectList的键+值对,这是不可能的。

1 个答案:

答案 0 :(得分:0)

@Html.Partial("~/Views/Shared/EditorTemplates/CustomDropdown.cshtml", CustomerCountrySelectionList)

模型直接进入视野。

@Html.Editor(),尽管有6次重载,但可能没有设计用于在自定义编辑器中传递模型。