gpt4 book ai didi

c# - ASP MVC4 的下拉列表

转载 作者:太空宇宙 更新时间:2023-11-03 20:12:28 26 4
gpt4 key购买 nike

我收到此错误“CS1928:

'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"

我的 Controller 看起来像这样

    public class IndexController : Controller
{
public ActionResult Index()
{
EpfeSelectScreen model = new EpfeSelectScreen();

var b = (from a in dbEntitiesErste.CONFIG_APPLICATIONS
orderby a.APPLICATION_ID
select new EPFE.CustomDataObjects.CustomObjects
{
Text = a.APPLICATION_NAME,
Value = a.APPLICATION_ID
});

model.Application = b.OrderBy(x => x.Text).ToList();

return View(model);




}
}

我的模型是这样的

    public class EpfeSelectScreen
{
public string Search { get; set; }
public string selectedApplication { get; set; }
public List<SelectListItem> Country { get; set; }
public List<CustomObjects> Application { get; set; }
public List<SelectListItem> MetaData { get; set; }
public List<SelectListItem> References { get; set; }
public List<SelectListItem> ReferencedBy { get; set; }
public List<SelectListItem> TreeView { get; set; }

public EpfeSelectScreen()
{
Country = new List<SelectListItem>();
Application = new List<CustomObjects>();
References = new List<SelectListItem>();
ReferencedBy = new List<SelectListItem>();
TreeView = new List<SelectListItem>();
}
}

我的 CustomObjects 是这个

    public class CustomObjects
{
public string Text { get; set; }
public short Value { get; set; }
}

我的 model.Application 中有一条记录,但是当该数据传递到我的 View 时,我得到了那个错误。

我的 View 看起来像这样

@model EPFE.Controllers.EpfeSelectScreen
@Html.DropDownListFor(m => m.selectedApplication, Model.Application)

如何解决这个问题?我究竟做错了什么?当我尝试使用 ListBoxFor 时出现同样的错误。

最佳答案

DropDownListFor 在传递 List 时将 SelectList 作为第二个参数。

你可以有类似下面的东西:

@Html.DropDownListFor(m => m.selectedApplication, 
new SelectList(Model.Application,"Value","Text"))

关于c# - ASP MVC4 的下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19111385/

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