本文介绍了绑定DropdownlistFor与Viewbag的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想绑定的DropDownListFor助手与控制器中定义的viewbag。但我正在逐渐错误。
查看code: -
@ Html.DropDownListFor(型号=> model.CurrencyID,ViewBag.CurrencyList为SelectListItem))
控制器code: -
public ActionResult Create()
> {
> var content = from p in db.Currency
> where p.IsActive == true
> orderby p.CurrencyName
> select new { p.CurrencyID, p.CurrencyCode };
>
> var x = content.ToList().Select(c => new SelectListItem
> {
> Text = c.CurrencyCode,
> Value = c.CurrencyID.ToString(),
> Selected = (c.CurrencyID == 68)
> }).ToList();
> ViewBag.CurrencyList = x;
>
> return View();
> }
Error Received:-System.Web.Mvc.HtmlHelper' does not contain a definition for 'DropDownListFor' and the best extension method overload 'System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>, System.Collections.Generic.IEnumerable)' has some invalid arguments
解决方案
You need to change
@Html.DropDownListFor(model => model.CurrencyID, ViewBag.CurrencyList as SelectListItem))
to
@Html.DropDownListFor(model => model.CurrencyID, ViewBag.CurrencyList as IEnumerable<SelectListItem>)
这篇关于绑定DropdownlistFor与Viewbag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!