模型为List时如何使用DropDownListFor

时间:2014-10-16 13:27:28

标签: c# model-view-controller html.dropdownlistfor

我的模型是通用列表。我想使用DropDownList进行外键属性绑定。 我的代码是

模型

public class PersonViewModel
{
    public int Id { get; set; }
    public string LastName{get;set;}
    public int NationalityId { get; set; }
}

public class Nationality
{
    public int Id { get; set; }
    public string Name { get; set; }
}

控制器

public ActionResult Index()
{

    var nationalities = new List<Nationality>
    {
        new Nationality{Id = 0, Name = "CHOOSE NATIONALITY..."},
        new Nationality{Id = 1, Name = "POLAND"},
        new Nationality{Id = 2, Name = "USA"},
        new Nationality{Id = 3, Name = "CANADA"}
    };


    var Nationalities = new SelectList(nationalities, "Id", "Name");


    var persons = new List<PersonViewModel>
    {
        new PersonViewModel{Id = 1, LastName = "KOWALSKI", NationalityId = 1},
        new PersonViewModel{Id = 1, LastName = "SMITH", NationalityId = 2},
        new PersonViewModel{Id = 1, LastName = "SCHERBATSKY", NationalityId = 3}
    };

    ViewBag.Nationalities = Nationalities;

    return View(persons);
}

查看

@model List<PersonViewModel>

@Html.EditorFor(m=> Model[0].LastName)
@Html.DropDownListFor(m => Model[0].NationalityId, (SelectList)ViewBag.Nationalities)

@Html.EditorFor(m => Model[1].LastName)
@Html.DropDownListFor(m => Model[1].NationalityId, (SelectList)ViewBag.Nationalities)

@Html.EditorFor(m => Model[2].LastName)
@Html.DropDownListFor(m => Model[2].NationalityId, (SelectList)ViewBag.Nationalities)

任何人都可以告诉我为什么EthnicId属性不能很好地绑定

我无法上传图片,但根本没有约束力。每个DropDownList都有选择国籍......&#39;

3 个答案:

答案 0 :(得分:1)

我建议您使用编辑器模板: 首先 - 创建包含以下内容的视图Shared\EditorTemplates\PersonViewModel.cshtml

@model PersonViewModel

@Html.EditorFor(m => Model.LastName) 
@Html.DropDownListFor(m => Model.NationalityId, (SelectList)ViewBag.Nationalities)

在您当前的视图中

@for (int i = 0; i < Model.Count; i++)
{
     @Html.EditorFor(x => Model[i])
}

答案 1 :(得分:0)

将您的视图模型更改为此

public class PersonViewModel
{
    public int Id { get; set; }
    public string LastName{get;set;}
    public int NationalityId { get; set; }
    public IEnumerable<Nationality> Nationalities {get;set;}
}

在您的控制器中

public ActionResult Index()
{

    var nationalities = new List<Nationality>
    {
        new Nationality{Id = 0, Name = "CHOOSE NATIONALITY..."},
        new Nationality{Id = 1, Name = "POLAND"},
        new Nationality{Id = 2, Name = "USA"},
        new Nationality{Id = 3, Name = "CANADA"}
    };


    var nationalities = new SelectList(nationalities, "Id", "Name");


    var persons = new List<PersonViewModel>
    {
        new PersonViewModel{Id = 1, LastName = "KOWALSKI", NationalityId = 1 , Nationalities = nationalities},
        new PersonViewModel{Id = 1, LastName = "SMITH", NationalityId = 2, Nationalities = nationalities},
        new PersonViewModel{Id = 1, LastName = "SCHERBATSKY", NationalityId = 3, Nationalities = nationalities}
    };    
    return View(persons);
}

在你看来

@model List<PersonViewModel>

@Html.EditorFor(m => m[0].LastName)
@Html.DropDownListFor(m => m[0].NationalityId, Model[0].Nationalities)

@Html.EditorFor(m => m[1].LastName)
@Html.DropDownListFor(m => m[1].NationalityId, Model[1].Nationalities)

@Html.EditorFor(m => m[2].LastName)
@Html.DropDownListFor(m => m[2].NationalityId, Model[2].Nationalities)

答案 2 :(得分:0)

两件事:

  1. 每个下拉列表都需要自己的选择列表。目前,您为每个下拉列表使用相同的选择列表。
  2. 您需要为每个选择列表指定一个选定的值,如下所示:

    new SelectList(nationalities, "Id", "Name", persons[0].NationalityId);