gpt4 book ai didi

c# - 如何在 ASP.NET MVC3 中创建下拉列表?

转载 作者:行者123 更新时间:2023-11-30 15:04:43 26 4
gpt4 key购买 nike

我正在尝试根据链接到模型的允许值列表在 ASP.NET MVC3 View 中创建下拉列表。

到目前为止,在我的模型中,我有:

namespace NS 
{
public class Model
{
public Model()
{
Status = new List<SelectListItem>();
Status.Add(new SelectListItem { Text = "New", Value = "New" });
Status.Add(new SelectListItem { Text = "PaymentPending", Value = "PaymentPending" });
Status.Add(new SelectListItem { Text = "PaymentProcessed", Value = "PaymentProcessed" });
Status.Add(new SelectListItem { Text = "Dispatched", Value = "Dispatched" });
Status.Add(new SelectListItem { Text = "Complete", Value = "Complete" });
Status.Add(new SelectListItem { Text = "Cancelled", Value = "Cancelled" });
}

public List<SelectListItem> Status { get; set; }
} // class Model
} // NS

(显然是去掉了不必要的东西)

然后在我看来我有:

@model NS.Model
@Html.DropDownListFor(Model.Status)

正如在 SO 上查看答案所暗示的那样。但是我得到一个错误:

Compiler Error Message: CS1501: No overload for method 'DropDownListFor' takes 1 arguments

非常感谢任何提示。

最佳答案

错误信息是不言自明的,DropDownListFor helper接受两个参数。

修改您的模型使其具有包含所选值的属性

public class Model
{
public Model() {
Status = new List<SelectListItem>();
Status.Add(new SelectListItem { Text = "New", Value = "New" });
Status.Add(new SelectListItem { Text = "PaymentPending", Value = "PaymentPending" });
Status.Add(new SelectListItem { Text = "PaymentProcessed", Value = "PaymentProcessed" });
Status.Add(new SelectListItem { Text = "Dispatched", Value = "Dispatched" });
Status.Add(new SelectListItem { Text = "Complete", Value = "Complete" });
Status.Add(new SelectListItem { Text = "Cancelled", Value = "Cancelled" });
}
public List<SelectListItem> Status { get; set; }
public string SelectedVal{get;set;}
}

然后在 View 中

@NS.Model
@Html.DropDownListFor(x=> x.SelectedVal, x.Status)

关于c# - 如何在 ASP.NET MVC3 中创建下拉列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9861535/

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