gpt4 book ai didi

asp.net-mvc - 将 DropdownlistFor 与 Viewbag 绑定(bind)

转载 作者:行者123 更新时间:2023-12-04 01:29:18 25 4
gpt4 key购买 nike

我正在尝试将 DropDownListFor 助手与 Controller 中定义的 viewbag 绑定(bind)。但我收到错误。

查看代码:-

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

Controller 代码:-


 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();
> }

收到错误:-
System.Web.Mvc.HtmlHelper'不包含'DropDownListFor'的定义和最佳扩展方法重载'System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions. Expression>, System.Collections.Generic.IEnumerable)' 有一些无效参数

最佳答案

你需要改变

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


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

关于asp.net-mvc - 将 DropdownlistFor 与 Viewbag 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20563801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com