MVC4中的WebGrid Dropdownlist错误

时间:2013-03-05 09:41:48

标签: asp.net-mvc-4 webgrid

我正在尝试使用下拉列表开发WebGrid,默认情况下,所选值http://www.mikesdotnetting.com/Article/202/Inline-Editing-With-The-WebGrid

// Controller中的代码(可以抓取多组数据):

ViewBag.material_codes = new SelectList(db.Master_Material, "material_code", "material_code");

// View中的代码(WebGrid):

grid.Column("material_code", MenuItemModel.translateMenuItem("MaterialCode"), format: 
                            @<text>
                                 @Html.DropDownList("material_code", (SelectList)ViewBag.material_code, item.material_code)
                            </text>),

然而我收到错误:

 Error  1   'System.Web.Mvc.HtmlHelper<IMv3.ViewModels.RMReceivingViewModels>' has no applicable method named 'DropDownList' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.  

我相信它是由“item.material_code”引起的,任何想法的人?

1 个答案:

答案 0 :(得分:0)

扩展方法(例如DropDownList)不能包含动态参数。因此,您需要将item.material_code强制转换为其基础类型:

@Html.DropDownList(
    "material_code", 
    (SelectList)ViewBag.material_code, 
    (string)item.material_code
)

在这里我将其转换为字符串,但如果基础类型不同,则应调整代码。