如何解决单击创建新孔时,希望能够选择一个CourseId
我正在尝试创建一个高尔夫比分应用程序。我创建了具有以下表格“ Round”,“ Course”和“ Hole”的数据库。
孔表具有一个指向courseId的外键,就像在创建每个孔时一样,我希望将其链接到课程。
当单击“新建”时,我希望有一个下拉列表,用于在输入所有孔的详细信息时选择CourseId。
但是当我单击“新建”时,在“孔”视图模型中出现以下错误。
System.ArguementNullException:值不能为Null。参数名称项。在HoleviewmodelController内的“创建动作”代码中。
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()
<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,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>
解决方法
好,所以我通过修改ActionResult创建代码来解决了此问题
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,"CourseName");
return View(viewCourse);
}
这允许填充下拉列表并将其添加到视图中。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。