DropDownListFor-模型绑定

DropDownListFor-模型绑定

问题描述:

我的第一个MVC项目遇到了一个问题,希望有人可以提供帮助.

My first MVC project and have ran into an issue which I hope someone can assist with.

基本上,我有一个DropDownListFor对象,该对象要填充可用时间列表,以供用户从中选择并将其存储在属性中,以供以后使用.

Basically I have a DropDownListFor object which I want to populate with a list of available times for the user to pick from and store the selected item in an attribute for later consumption.

以下内容会产生空值错误,因此我遗漏了一些显而易见的内容,我们将不胜感激.

The below is producing a null value error so I'm missing something obvious, any help would be appreciated.

这就是我所拥有的:

控制器:

private MyModelObject m_model = new MyModelObject();
public ActionResult Index()
{
    var AvailTimes = new SelectList(new[]
    {
        new {Value="00:00",Text="12:00 AM"},
        new {Value="00:30",Text="12:30 AM"},
        new {Value="01:00",Text="1:00 AM"},
        new {Value="22:30",Text="10:30 PM"},
        new {Value="23:00",Text="11:00 PM"},
        new {Value="23:30",Text="11:30 PM"},                 
    }); 
    return View(m_model);
}

型号:

public class MyModelObject
{
    public string StartTime { get; set; }
    public IEnumerable<SelectList> AvailTimes { get; set; }
}

查看:

@Html.DropDownListFor(model => model.StartTime, 
                       new SelectList(model.AvailTimes, "Value", "Text"))

不要将模型设为控制器的私有变量.每次发送请求时,都会创建一个新的控制器实例,因此不要以为您可以在操作之间重用它.另外,对于在操作中声明的AvailTimes局部变量,您似乎没有做任何有用的事情,它会受到快速垃圾回收的影响.另外,您的模型不正确. AvailTimes属性必须是IEnumerable<SelectListItem>,而不是IEnumerable<SelectList>.

Don't make your model a private variable to the controller. Each time a request is sent a new controller instance will be created so don't think that you will be able to reuse it between actions. Also you don't seem to be doing anything useful with the AvailTimes local variable that you declared in your action and it is subject to a fast garbage collection. Also your model is incorrect. The AvailTimes property must be an IEnumerable<SelectListItem> and not IEnumerable<SelectList>.

让我们首先修复视图模型:

Let's start by fixing the view model first:

public class MyModelObject
{
    public string StartTime { get; set; }
    public IEnumerable<SelectListItem> AvailTimes { get; set; }
}

然后执行将为视图模型提供数据的控制器操作:

then the controller action which will feed the view model:

public ActionResult Index()
{
    var model = MyModelObject
    {
        AvailTimes = new[]
        {
            new SelectListItem { Value = "00:00", Text = "12:00 AM" },
            new SelectListItem { Value = "00:30", Text = "12:30 AM" },
            new SelectListItem { Value = "01:00", Text = "1:00 AM" },
            new SelectListItem { Value = "22:30", Text = "10:30 PM" },
            new SelectListItem { Value = "23:00", Text = "11:00 PM" },
            new SelectListItem { Value = "23:30", Text = "11:30 PM" },                 
         } 
    };
    return View(model);
}

最后是强类型视图:

@model MyModelObject
@Html.DropDownListFor(x => x.StartTime, Model.AvailTimes)

您还可以直接在视图模型中分配这些可用时间:

You also have the possibility to assign those available hours in your view model directly:

public class MyModelObject
{
    public string StartTime { get; set; }
    public IEnumerable<SelectListItem> AvailTimes 
    { 
        get
        {
            return new[]
            {
                new SelectListItem { Value = "00:00", Text = "12:00 AM" },
                new SelectListItem { Value = "00:30", Text = "12:30 AM" },
                new SelectListItem { Value = "01:00", Text = "1:00 AM" },
                new SelectListItem { Value = "22:30", Text = "10:30 PM" },
                new SelectListItem { Value = "23:00", Text = "11:00 PM" },
                new SelectListItem { Value = "23:30", Text = "11:30 PM" },                 
             }; 
        }
    }
}

现在您的控制器动作可能变为:

Now your controller action could become:

public ActionResult Index()
{
    var model = MyModelObject();
    return View(model);
}

该视图显然保持不变.