创建模型类和子项问题

时间:2010-02-19 13:02:51

标签: asp.net-mvc

我有一个Asp.net mvc应用程序,我需要一个创建页面。 我的班级为;

 public class OrderItemModel
        {
            public OrderItemModel()
            {
                SelectedItems = new List<MenuItem> ;
            }

            public List<MenuItem> SelectedItems { get; set; }
            public string Address { get; set; }
            public string Notlar { get; set; }
            public string Odeme { get; set; }
        }

如何设置SelectedItems属性。 ?我无法弄清楚子项目的添加。

有关于列表属性的任何想法或示例吗?

2 个答案:

答案 0 :(得分:1)

<input type="text" name="SelectedItems[0].SomePropOfMenuItem" value="" />
<input type="text" name="SelectedItems[1].SomePropOfMenuItem" value="" />

答案 1 :(得分:1)

使用SelectListItems而不是MenuItem:

    public class OrderItemModel
{
    public OrderItemModel()
        {
            SelectedItems = new List<SelectListItem>();
        }

    public List<SelectListItem> SelectedItems { get; set; }
    public string Address { get; set; }
    public string Notlar { get; set; }
    public string Odeme { get; set; }
}

然后,在您的控制器中执行:

public ActionResult Index()
{
    OrderItemModel model = new OrderItemModel() 
    {
        Address = "blah",
        Notlar = "blah",
        Odeme = "blah"
    };

    for (int i = 0; i < 10; i++)
        model.SelectedItems.Add(new SelectListItem() { Text = "Option " + i, Value = i.ToString(), Selected = i == 5 });

    return View(model);
}

并在您的视图中

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.OrderItemModel>" %>
...blah blah...
<%= Html.DropDownList("MyDropDown", Model.SelectedItems) %>