绑定DropdownlistFor与Viewbag [英] Bind DropdownlistFor with the Viewbag

查看:136
本文介绍了绑定DropdownlistFor与Viewbag的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绑定的DropDownListFor助手与控制器中定义的viewbag。但我正在逐渐错误。

查看code: -

  @ Html.DropDownListFor(型号=> model.CurrencyID,ViewBag.CurrencyList为SelectListItem))

控制器code: -


  


 公众的ActionResult的Create()
> {
> VAR内容=由对在db.Currency
>其中,p.IsActive ==真
>排序依据p.CurrencyName
>选择新的{p.CurrencyID,p.Currency code};
>
> 。VAR X = content.ToList()选择(C =>新建SelectListItem
> {
>文本= c.Currency code,
>值= c.CurrencyID.ToString(),
>选择=(c.CurrencyID == 68)
> })了ToList()。
> ViewBag.CurrencyList = X;
>
>返回查看();
> }

收到

错误: -
System.Web.Mvc.HtmlHelper'不包含定义'DropDownListFor'和最佳推广方法重载'System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper,System.Linq.Ex pressions.Ex pression>,System.Collections.Generic.IEnumerable)'有一些无效参数


解决方案

您需要修改

  @ Html.DropDownListFor(型号=> model.CurrencyID,ViewBag.CurrencyList为SelectListItem))

  @ Html.DropDownListFor(型号=> model.CurrencyID,ViewBag.CurrencyList为IEnumerable< SelectListItem>)

I am trying to bind the DropDownListFor helper with the viewbag defined in the controller. But i am getting error.

View Code:-

@Html.DropDownListFor(model => model.CurrencyID, ViewBag.CurrencyList as SelectListItem)) 

Controller 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆