C# DropDownListFor将值为NULL的post值发送到数据库

C# DropDownListFor将值为NULL的post值发送到数据库,c#,model-view-controller,razor,C#,Model View Controller,Razor,我在SQL中有一个课程表和一个洞表。孔表中有一个外键设置为CourseId 这样,当我创建一个孔时,我可以将其指定给相关课程 在我看来,我有一个DropDownList for,它有一个创建孔时可供选择的球场列表。但是,当我尝试发回值时,courseID不会发回 HoleViewModel // GET: HoleViewModels/Create public ActionResult Create() { var dbcourse = db.Course.T

我在SQL中有一个课程表和一个洞表。孔表中有一个外键设置为CourseId

这样,当我创建一个孔时,我可以将其指定给相关课程

在我看来,我有一个DropDownList for,它有一个创建孔时可供选择的球场列表。但是,当我尝试发回值时,courseID不会发回

HoleViewModel

 // GET: HoleViewModels/Create
    public ActionResult Create()
    {
        var dbcourse = db.Course.ToList();

        //Make selectlist, which is IEnumerable<SelectListItem>
        var courseNameDropdownList = new SelectList(db.Course.Select(item => new SelectListItem()
        {
            Text = item.CourseName.ToString(),
            Value = item.CourseId.ToString()
        }).ToList(), "Value", "Text");

        // Assign the Selectlist to the View Model   
        var viewCourse = new HoleViewModel()
        {
            Course = dbcourse.FirstOrDefault(),
            // The Dropdownlist values
            CourseNamesDropdownList = courseNameDropdownList,

        };

        
        return View(viewCourse);
    }

    // POST: HoleViewModels/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "HoleId,HoleNumber,Par,Length,StrokeIndex,CourseId")]  HoleViewModel holeViewModel)
    {
        if (ModelState.IsValid)
        {
            db.HoleViewModels.Add(holeViewModel);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        
        return View(holeViewModel);
    }
{ @Html.AntiForgeryToken()


HoleViewModel

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.HoleNumber,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.HoleNumber,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.HoleNumber,“,new{@class=“text danger”}) @ HTML(LabelFor)(Model=> model .PAR,HTMLATP:新的{@类=“控制标签COL-MD-2”}) @ html(EditorFor)(model = > model .PAR,新{HTMLATRATITES =新{{Cype=“窗体控件”}) @ html(ValidationMessageFor)(model = > model .PAR,“”,新{@类=“文本危险”}) @LabelFor(model=>model.Length,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Length,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Length,“,new{@class=“text danger”}) @LabelFor(model=>model.StrokeIndex,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.StrokeIndex,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.StrokeIndex,“,new{@class=“text danger”}) @LabelFor(model=>model.Course.CourseId,“CourseId”,htmlAttributes:new{@class=“controllabel col-md-2”}) @Html.DropDownListFor(model=>model.Course.CourseId,model.CourseNamesDropdownList,“请从列表中选择”,新建{@class=“form control”}) @Html.ValidationMessageFor(model=>model.CourseId,“,new{@class=“text danger”})

这样,当HaveVIEW发布回HONENUM、PAR、长度、SPLKEDISTIC、CURIDID时,它将所有数据传递到孔表之外,而不是它所发布的NULL ID。 要将CourseId输入数据库,我在帖子中遗漏了什么

@using (Html.BeginForm()) 
<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.Course.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>