公开课课程{公共 int CourseId { 获取;放;}公共字符串名称 { 获取;放;}公共 int Par { 得到;放;}公共字符串 Tee { get;放;}公共十进制评级{得到;放;}公共虚拟 IEnumerable孔{得到;放;}公共静态课程创建(){var course = new Course();course.Par = 72;varholes = new List();for (int i = 0; i <18; i++){holes.Add(new CourseHole() { Course = course, Number = i + 1, Par = 4 });}course.Holes = 洞;回程;}}公开课课程洞{公共 int CourseHoleId { 获取;放;}public int Number { 得到;放;}公共 int Par { 得到;放;}公共 int 长度 { 得到;放;}公共 int 排名 { 得到;放;}公共课程课程{得到;放;}}
以及以下父视图.
@model Golf_Statz.Models.Course@{ViewBag.Title = "创建";}<h2>创建</h2>@using (Html.BeginForm()){@Html.AntiForgeryToken()<div class="form-horizontal"><h4>课程</h4><小时/>@Html.ValidationSummary(true, "", new { @class = "text-danger" })<div class="form-group">@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-4">@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Par, htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-4">@Html.EditorFor(model => model.Par, new { htmlAttributes = new { @class = "form-control" } })@Html.ValidationMessageFor(model => model.Par, "", new { @class = "text-danger" })
<div class="form-group">@Html.LabelFor(model => model.Tee, htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-4">@Html.EditorFor(model => model.Tee, new { htmlAttributes = new { @class = "form-control" } })@Html.ValidationMessageFor(model => model.Tee, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Rating, htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-4">@Html.EditorFor(model => model.Rating, new { htmlAttributes = new { @class = "form-control" } })@Html.ValidationMessageFor(model => model.Rating, "", new { @class = "text-danger" })
I am using a partial view to create a parent child view. What I would ideally like is the submit button on the parent view to save the child values.
I have the following model.
public class Course
{
public int CourseId { get; set; }
public string Name { get; set; }
public int Par { get; set; }
public string Tee { get; set; }
public decimal Rating { get; set; }
public virtual IEnumerable<CourseHole> Holes { get; set; }
public static Course Create()
{
var course = new Course();
course.Par = 72;
var holes = new List<CourseHole>();
for (int i = 0; i < 18; i++)
{
holes.Add(new CourseHole() { Course = course, Number = i + 1, Par = 4 });
}
course.Holes = holes;
return course;
}
}
public class CourseHole
{
public int CourseHoleId { get; set; }
public int Number { get; set; }
public int Par { get; set; }
public int Length { get; set; }
public int Ranking { get; set; }
public Course Course { get; set; }
}
// GET: Course/Create
public ActionResult Create()
{
return View(Course.Create());
}
// POST: Course/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "CourseId,Name,Par,Tee,Rating")] Course course)
{
if (ModelState.IsValid)
{
db.Courses.Add(course);
foreach (var hole in course.Holes)
{
db.CourseHoles.Add(hole);
}
db.SaveChanges();
return RedirectToAction("Index");
}
return View(course);
}
No matter what I seem to do course.Holes in the HttpPost Create method is always null.
I think I want something similar to this but it's for editing and I want it for creating.
Any help would be greatly appreciated as this is my first mvc project.
解决方案
Your foreach loop is generating duplicate id attributes (invalid html) and name attributes which have no relationship to your model. Change the partial to an EditorTemplate
/Views/Shared/EditorTemplates/CourseHole.cshtml
and remove the BeginForm, AntiForgeryToken and scripts