asp.net mvc父子视图,父视图更新子项

我正在使用局部视图来创建父子视图。 理想情况下,父视图上的提交按钮用于保存子值。

我有以下型号。

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 Holes { get; set; } public static Course Create() { var course = new Course(); course.Par = 72; var holes = new List(); 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; } } 

以及以下父视图。

 @model Golf_Statz.Models.Course @{ ViewBag.Title = "Create"; } 

Create

@using (Html.BeginForm()) { @Html.AntiForgeryToken()

Course


@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
@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" })
@Html.EditorFor(model => model.Par, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Par, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Tee, htmlAttributes: new { @class = "control-label col-md-2" })
@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" })
@Html.EditorFor(model => model.Rating, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Rating, "", new { @class = "text-danger" })
@foreach (Golf_Statz.Models.CourseHole hole in Model.Holes) { @Html.Partial("CreateHole", hole) }
}
@Html.ActionLink("Back to List", "Index")
@section Scripts { @Scripts.Render("~/bundles/jqueryval") }

以及部分视图。

 @model Golf_Statz.Models.CourseHole @{ ViewBag.Title = "CreateHole"; } @using (Html.BeginForm()) { @Html.AntiForgeryToken() 
@Html.ValidationSummary(true, "", new { @class = "text-danger" })

@Model.Number

@Html.EditorFor(model => model.Par, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Par, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Length, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Length, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Ranking, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Ranking, "", new { @class = "text-danger" })
} @section Scripts { @Scripts.Render("~/bundles/jqueryval") }

我的控制器方法是。

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

无论我怎么做,HttpPost Create方法中的Hles始终为null。 我想我想要类似的东西,但它是用于编辑,我希望它用于创建。 任何帮助将不胜感激,因为这是我的第一个mvc项目。

你的foreach循环正在生成重复的id属性(无效的html)和与你的模型没有关系的name属性。 将partial更改为EditorTemplate

/Views/Shared/EditorTemplates/CourseHole.cshtml

并删除BeginFormAntiForgeryToken和脚本

 @model Golf_Statz.Models.CourseHole 

@Model.Number

@Html.EditorFor(model => model.Par, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Par, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Length, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Length, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Ranking, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Ranking, "", new { @class = "text-danger" })

然后在主视图中

 @Html.EditorFor(m => m.Holes) 

例如, EditorFor()方法将正确生成用于绑定到集合的html

   

您还需要删除[Bind]属性,因为您要排除属性Holes ,并且您似乎想要绑定到所有属性。

附注:您没有为孔CourseHoleIdNumber属性生成输入,因此这些不会回发。

在你的情况下,你有两种forms! 剃刀会产生这个

 

从局部视图中删除@using (Html.BeginForm()) {}