【问题标题】:WebGrid Dropdownlist error in MVC4MVC4 中的 WebGrid 下拉列表错误
【发布时间】:2013-03-05 09:41:48
【问题描述】:

我正在尝试开发一个带有下拉列表的 WebGrid,默认情况下选择值 http://www.mikesdotnetting.com/Article/202/Inline-Editing-With-The-WebGrid

//Controller中的代码(抓取超过1组数据):

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

//视图中的代码(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”引起的,有什么想法吗?

【问题讨论】:

    标签: asp.net-mvc-4 webgrid


    【解决方案1】:

    扩展方法(例如DropDownList)不能包含动态参数。所以你需要将item.material_code 转换为它的底层类型:

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

    这里我将它转换为字符串,但如果底层类型不同,你应该调整你的代码。

    【讨论】:

    • 但还有一件事:该值未绑定在下拉列表中,这意味着当我选择例如。 “1”,当我展开时,下拉列表中会有重复的“1”值。有什么想法吗?
    • 此外,在 ViewBag.material_codes = new SelectList(db.Master_Material, "material_code", "material_code");我没有包含 selectedValue 因为我想获取超过 1 个值,我相信我必须在某处循环
    猜你喜欢
    • 1970-01-01
    • 2013-05-22
    • 1970-01-01
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多