C# 单击“创建新孔”时,希望能够选择一个孔ID

C# 单击“创建新孔”时,希望能够选择一个孔ID,c#,html,asp.net-mvc,entity-framework,C#,Html,Asp.net Mvc,Entity Framework,我正在尝试创建一个高尔夫记分应用程序。我已经创建了一个数据库,其中包含以下表格:Round、Course和Hole 当我创建每个孔时,孔表有一个courseId的外键,我想将其链接到一个课程 当点击“新建”按钮时,我希望有一个下拉列表,在输入所有孔详细信息时从中选择courseId 但是,当我单击“新建”按钮时,孔视图模型中会出现以下错误 System.ArguementNullException:值不能为Null。参数名称项。在我的中,在HoleViewModelController中创建操作

我正在尝试创建一个高尔夫记分应用程序。我已经创建了一个数据库,其中包含以下表格:Round、Course和Hole

当我创建每个孔时,孔表有一个courseId的外键,我想将其链接到一个课程

当点击“新建”按钮时,我希望有一个下拉列表,在输入所有孔详细信息时从中选择courseId

但是,当我单击“新建”按钮时,孔视图模型中会出现以下错误

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


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.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”})
好的,所以我通过修改ActionResult创建代码解决了这个问题

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

public ActionResult Create()
{
var dbcourse=db.Course.ToList();
//将selectlist设置为IEnumerable
var courseNameDropdownList=new SelectList(db.Course.Select(item=>new SelectListItem())
{
Text=item.CourseName.ToString(),
Value=item.CourseId.ToString()
}).ToList(),“值”,“文本”);
//将Selectlist指定给视图模型
var viewCourse=新的HoleViewModel()
{
Course=dbcourse.FirstOrDefault(),
//下拉列表的值
courseNameDropdownList=courseNameDropdownList,
};
//ViewBag.CourseId=新选择列表(db.CourseViewModels,“CourseId”,“CourseName”);
返回视图(viewCourse);
}

这允许填充下拉列表并将其添加到视图中。

看起来
db.CourseViewModels
为空。我可以看到错误表明db.CourseViewModel为空,但是数据库中有一个值,因此不理解它为什么说它为空,或者我如何获得要显示的值。创建孔时re将是分配给许多不同课程的选项。您在何处实例化了
db
?在HoleViewController的顶部,我有以下代码private MyGolfScoreAppDb=new MyGolfScoreAppDb();好吧,这有点奇怪。我不确定那是怎么回事,tbh。也许有人有更丰富的EF经验会帮上忙。
        public ActionResult Create()
{
    ViewBag.CourseId = new SelectList(db.CourseViewModels, "CourseId", "CourseName");
    return View();
}
        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,

        };

        //ViewBag.CourseId = new SelectList(db.CourseViewModels, "CourseId", "CourseName");
        return View(viewCourse);
    }