DropdownListFor中的实体关系

时间:2013-09-10 15:35:25

标签: c# asp.net-mvc-4 razor

目前我有以下实体:
电影:

public class Movie
{

    public int ID { get; set; }
    [Required]
    public string Title { get; set; }
    [DataType(DataType.Date)]
    public DateTime ReleaseDate { get; set; }
    [Required]
    public string Genre { get; set; }
    [DataType(DataType.Currency), Range(1,100)]
    public decimal Price { get; set; }
    [StringLength(5)]
    public string Rating { get; set; }
    [StringLength(200)]
    public string comments { get; set; }


    public Director director { get; set; }
}

导演:

[Table("Director")]
public class Director
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    [Required, MaxLength(50)]
    [Display(Name = "Name")]
    public string name { get; set; }
    [Required, MaxLength(50)]
    [Display(Name = "Last Name")]
    public string lastName { get; set; }
    [Required, Range(1,100)]
    public int age { get; set; }

    [NotMapped]
    public string fullName {
        get { return this.name + " " + this.lastName; }
    }
}

当我访问编辑表单时,我从DB中的Directors加载SelectList:

public ActionResult Edit(int id = 0)
    {
        Movie movie = db.Movies.Find(id);
        if (movie == null)
        {
            return HttpNotFound();
        }
        ViewBag.Directors = new SelectList(db.Directors.ToList(), "ID", "fullname");
        return View(movie);
    }

然后我使用@ Html.DropDownListFor来显示表单中的组合框:

 <div class="editor-field">
        @Html.DropDownListFor(model => model.director, (SelectList)ViewBag.Directors, "Select")
        @Html.ValidationMessageFor(model => model.director)
    </div>

一切似乎都没问题,但是当我选择一个选项并尝试保存表单时,我收到一条验证消息,说“值'1'无效”。其中“1”是董事ID。

所以,我肯定错过了直接绑定导演的东西。

2 个答案:

答案 0 :(得分:0)

如果您不想按惯例修改ID,那么您可能会更改代码,这会创建SelectList:

var list = new List<SelectListItem>;

var items = from n in db.Directors
            select new SelectListItem
            {
              Text = n.fullname,
              Value = n.ID
             };
//*Add your items to List
foreach (var item in items)
    list.Add(item);

ViewBag.Directors = list;

<强>编辑: 别看我的第一部分。评论员是对的。您的型号未连接。您需要directorID模型中的Movie属性才能使其正常运行。你应该改变你的DropDownListFor如下:

@Html.DropDownListFor(model => model.directorID, (SelectList)ViewBag.Directors, "Select")

答案 1 :(得分:0)

您的实体是自动生成的,还是您自己创建的。 如果您已自动生成,则看起来您与Director和Movie之间没有关系。否则,您的电影将具有属性DirectorID,而目录实体将具有ICollection of Movies。 此外,在Director的电影中使用DropDownlistFor时,你应该使用DirectorID,这将是正确的方法。 即。

public class Movie
{

public int ID { get; set; }
[Required]
public string Title { get; set; }
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
[Required]
public string Genre { get; set; }
[DataType(DataType.Currency), Range(1,100)]
public decimal Price { get; set; }
[StringLength(5)]
public string Rating { get; set; }
[StringLength(200)]
public string comments { get; set; }
//change here
public int DirectorID {get; set;}

public Director director { get; set; }
}

选择列表将是

<div class="editor-field">
    @Html.DropDownListFor(model => model.DirectorID, (SelectList)ViewBag.Directors, "Select")
    @Html.ValidationMessageFor(model => model.DirectorID)
</div>

要填充导演,您可以按

创建一个选择列表
var directors=db.Directors.Select(o=>new {ID=o.DirectorID, Name=o.DirectorName});

并像往常一样将此选择列表添加到viewbag。

ViewBag.Directors = new SelectList(directors, "ID", "Name");