0

I am trying to develop a WebGrid with dropdownlist, where by default the selected value http://www.mikesdotnetting.com/Article/202/Inline-Editing-With-The-WebGrid

//code in Controller (which grabs more than 1 set of data):

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

//code in View(WebGrid):

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

However I get the error:

 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.  

I believe it is caused by "item.material_code", any idea guys?

MiaoWin
  • 475
  • 2
  • 6
  • 14

1 Answers1

0

Extension methods (such as the DropDownList) cannot contain dynamic parameters. So you need to cast the item.material_code to its underlying type:

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

Here I cast it to string but if the underlying type is different you should adjust your code.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • But 1 more thing: the value is not binded in the drop down list, which means when I select eg. "1", there will be duplicate "1" value in the drop down list when i expand. Any idea? – MiaoWin Mar 06 '13 at 04:46
  • besides, in ViewBag.material_codes = new SelectList(db.Master_Material, "material_code", "material_code"); I did not include selectedValue because I want to grab more than 1 value, I believe I have to do loop somewhere – MiaoWin Mar 06 '13 at 04:46