【问题标题】:Scaffolding models does not display specific column from row脚手架模型不显示行中的特定列
【发布时间】:2018-11-03 23:18:49
【问题描述】:

我有下表:

以及它的模型:

 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,并且应该带有标题:

我该如何改变呢?

【问题讨论】:

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


    【解决方案1】:

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

    查看

    @*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});
    

    【讨论】:

    • 我相信这适用于 MVC,它如何与 Razor Pages 一起使用?
    • 我不知道是剃须刀页面,你能把你的cshtml文件也显示一下吗?
    猜你喜欢
    • 1970-01-01
    • 2015-03-03
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    相关资源
    最近更新 更多