脚手架模型不显示行中的特定列

时间:2018-05-24 16:38:10

标签: c# razor asp.net-core model razor-pages

我有下表:

enter image description here

它的模型:

 public class Movie
{
    public int Id { get; set; }
    public string Title { get; set; }
    public int Duration { get; set; }

    public enum Ratings
    {
        G = 1,
        PG = 2,
        PG13 = 3,
        R = 4
    }

    public virtual Ratings MaturityRating { get; set; }

    public ICollection<Screening> Screenings { get; set; }
}

在我的屏幕模型中,我想引用其中一部电影,以便我可以添加一个屏幕:

  public class Screening
{
    public int Id { get; set; }
    public DateTime Date { get; set; }
    public int Room { get; set; }
    public int Seats { get; set; }

    [ForeignKey("Movie")]
    public virtual int MovieId { get; set; }
    public virtual Movie Movie { get; set; }

}

问题是,脚手架形式会在下拉列表中显示带有ID的电影,并且应该带有标题:

enter image description here

我该如何改变?

1 个答案:

答案 0 :(得分:0)

在控制器中创建一个列表以填充下拉列表。它的ViewBag对象名称与它将用于下拉项的Model属性名称匹配。

查看

@*DropDownList*@
<div class="form-group">
    @Html.LabelFor(model => model.Movie, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownListFor(model => model.Movie, null, "Please Select", new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.Movie, "", new { @class = "text-danger" })
    </div>
</div>

控制器操作,包括

ViewBag.Movie = db.Movies.GetAll().Select(x=> new SelectListItem() { Name = x.Name, Value x.Id});