Category Hierarchy

我正在尝试创建一个高尔夫得分应用程序。我已经创建了数据库,该数据库具有以下表:圆形、课程和孔。

孔参数表有一个指向courseId的外键,因为当我创建每个孔时,我想要将它链接到一个路线。

当点击create new时,我希望有一个下拉列表,在输入所有孔详细信息时可以从中选择courseId。

但是当我点击create new时,我在Hole视图模型中得到以下错误。

System.ArguementNullException:值不能为Null。参数名称项。在我的HoleViewModelController内创建操作代码中。

这是我的HoleViewModel代码

    public class HoleViewModel
{
    [Key]
    public int HoleId { get; set; }

    public int HoleNumber { get; set; }

    public int Par { get; set; }

    public int Length { get; set; }

    public int StrokeIndex { get; set; }

    [ForeignKey("Course")]
    public int? CourseId { get; set; }
    public IEnumerable<SelectListItem> CourseNamesDropdownList { get; set; }

    public virtual CourseViewModel Course { get; set; }
}

}

这是我的CourseViewModel代码

    public class CourseViewModel
{
    [Key]
    public int CourseId { get; set; }

    public string CourseName { get; set; }

这是我的HoleViewModelController创建操作

        public ActionResult Create()
    {
        ViewBag.CourseId = new SelectList(db.CourseViewModels, "CourseId", "CourseName");
        return View();
    }

这是我的MVC创建视图

@using (Html.BeginForm()) 

{ @Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>HoleViewModel</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.HoleNumber, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.HoleNumber, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.HoleNumber, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Par, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Par, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Par, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Length, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Length, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Length, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.StrokeIndex, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.StrokeIndex, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.StrokeIndex, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.CourseId, "CourseId", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.Course.CourseId, Model.CourseNamesDropdownList, "Please select from List", new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.CourseId, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>

转载请注明出处:http://www.tiantaijiaoyu.com/article/20230526/2043304.html