0

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

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; }
    }
}

当我访问编辑表单时,我从数据库中的 Director 加载一个 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。

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

4

2 回答 2

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")
于 2013-09-10T16:13:36.573 回答
0

您的实体是自动生成的,还是您自己制作的。如果你有自动生成的,那么导演和电影之间似乎没有关系。否则,您的电影将具有属性 DirectorID,并且目录实体将具有电影的 ICollection。此外,在导演的电影中使用 DropDownlistFor 时,您应该使用 DirectorID,这将是正确的方法。IE

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");
于 2013-09-11T06:16:18.117 回答