ASP.NET MVC 2:手动创建列表并附加到模型

发布于 2024-10-11 21:21:36 字数 1052 浏览 1 评论 0原文

我有一个模型:

[Required]
public List<SelectList> Meals { get; set; 

}

我想在我的控制器中创建一个列表,以便我可以将其附加到我的模型中的“餐食”。

由于某种原因,我在执行此操作时遇到了问题:

_model.MaxCoupons = new List<SelectListItem>();
SelectListItem _mList = new SelectListItem([]{
    new SelectListItem { Text = "---", Value = "" },
    new SelectListItem { Text = "50", Value = "50" },
    new SelectListItem { Text = "60", Value = "60" },
    new SelectListItem { Text = "70", Value = "70" },
    new SelectListItem { Text = "80", Value = "80" },
    new SelectListItem { Text = "90", Value = "90" },
    new SelectListItem { Text = "100", Value = "100" },
    new SelectListItem { Text = "110", Value = "110" },
    new SelectListItem { Text = "120", Value = "120" },
    new SelectListItem { Text = "130", Value = "130" },
    new SelectListItem { Text = "140", Value = "140" },
    new SelectListItem { Text = "150", Value = "130" } },
     "Text", "Value" );
_model.MaxCoupons.Add(_mList);

我现在有点困惑......

I have a model:

[Required]
public List<SelectList> Meals { get; set; 

}

And I want to create a list in my Controller so I can attach it to "Meals" in my Model.

For some reason I'm having issues doing this:

_model.MaxCoupons = new List<SelectListItem>();
SelectListItem _mList = new SelectListItem([]{
    new SelectListItem { Text = "---", Value = "" },
    new SelectListItem { Text = "50", Value = "50" },
    new SelectListItem { Text = "60", Value = "60" },
    new SelectListItem { Text = "70", Value = "70" },
    new SelectListItem { Text = "80", Value = "80" },
    new SelectListItem { Text = "90", Value = "90" },
    new SelectListItem { Text = "100", Value = "100" },
    new SelectListItem { Text = "110", Value = "110" },
    new SelectListItem { Text = "120", Value = "120" },
    new SelectListItem { Text = "130", Value = "130" },
    new SelectListItem { Text = "140", Value = "140" },
    new SelectListItem { Text = "150", Value = "130" } },
     "Text", "Value" );
_model.MaxCoupons.Add(_mList);

I'm a bit confused right now...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染年凉城似染瑾 2024-10-18 21:21:36
public SelectList Meals { get; set; }

public SelectList MySelectList()
{
  List<SelectListItem> _returnList = new List<SelectListItem>();

  SelectListItem _mList = new SelectListItem();
  _mList = new SelectListItem(){ Text = "---", Value = "" };
  _returnList.Add(_mList);

  //keep repeating
}

然后在一个方法中:

Meals = new MySelectList();
public SelectList Meals { get; set; }

public SelectList MySelectList()
{
  List<SelectListItem> _returnList = new List<SelectListItem>();

  SelectListItem _mList = new SelectListItem();
  _mList = new SelectListItem(){ Text = "---", Value = "" };
  _returnList.Add(_mList);

  //keep repeating
}

then inside a method:

Meals = new MySelectList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文