[英]MVC Partial View not rendering from an EditorTemplates
我正在使用MVC 5 / EF 6.1與父母(一對多)關系建立表單。 我正在嘗試通過局部視圖來呈現子級,該局部視圖不會呈現或給我任何錯誤(在創建新表單時)。 我嘗試運行調試器,但沒有發現任何錯誤(我確定有問題)。
到目前為止,這就是我所擁有的:
楷模
public class Parent
{
public int ParentID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<Child> Childs { get; set; }
}
public class Child
{
public int ChildID { get; set; }
public int ParentID { get; set; }
public string Name { get; set; }
public string DOB { get; set; }
public string Address { get; set; }
public virtual Parent Parent { get; set; }
}
視圖模型
public class ParentVM
{
public ParentVM()
{
//Children = new List<ChildVM>()
// {
// new ChildVM(){Name="1", DOB="1", Address="1"},
// new ChildVM(){Name="1", DOB="1", Address="1"},
// new ChildVM(){Name="1", DOB="1", Address="1"},
//};
Children = new List<ChildVM>();
}
public int ParentID { get; set; }
public int ChildID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Name { get; set; }
public string DOB { get; set; }
public string Address { get; set; }
public IList<ChildVM> Children { get; set; }
}
public class ChildVM
{
public int ChildID { get; set; }
public int ParentID { get; set; }
public string Name { get; set; }
public string DOB { get; set; }
public string Address { get; set; }
}
視圖(通過腳手架創建:索引,詳細信息,編輯,創建,刪除)
@model SomeNamespace.ViewModels.ParentVM
@{ViewBag.Title = "Create";}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Parent</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<table border="1">
<tr>
<th>Name</th>
<th>DOB</th>
<th>Address</th>
</tr>
@Html.EditorFor(x => x.Children)
</table>
<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>}
<div>@Html.ActionLink("Back to List", "Index")</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")}
局部視圖(Views / Shared / EditorTemplates / Child.cshtml)
@model SomeNamespace.Models.Child
<tr>
<td>
@Html.EditorFor(model => model.Name)
</td>
<td>
@Html.EditorFor(model => model.DOB)
</td>
<td>
@Html.EditorFor(model => model.Address)
</td>
控制器(ParentsController.cs)
// GET: Parents/Create
public ActionResult Create()
{
//var viewModel = new ParentVM
//{
// Children =
// new List<ChildVM>()
// {
// new ChildVM() {Name="1", DOB="1", Address="1"},
// new ChildVM() {Name="1", DOB="1", Address="1"},
// new ChildVM() {Name="1", DOB="1", Address="1"}
// }
//};
return View(new ParentVM());
//return View(viewModel);
}
// POST: Parents/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 = "ParentID,FirstName,LastName, Name")] ParentVM viewModel)
{
if (ModelState.IsValid)
{
var child = new Child()
{
//Name = viewModel.Name,
//DOB = viewModel.DOB,
//Address = viewModel.Address
};
var parent = new Parent()
{
//FirstName = viewModel.FirstName,
//LastName = viewModel.LastName
};
db.Parents.Add(parent);
db.Childs.Add(child);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(viewModel);
}
這是我在視圖源中看到的:
<table border="1">
<tr>
<th>Name</th>
<th>DOB</th>
<th>Address</th>
</tr>
</table>
也許它不喜歡這種類型? 我以為您可以在部分視圖中使用模型(editorTemplates)?
您的ParentVM
類包含一個屬性public IList<ChildVM> Children { get; set; }
public IList<ChildVM> Children { get; set; }
public IList<ChildVM> Children { get; set; }
但您的EditorTemplate
用於@model SomeNamespace.Models.Child
。 更改為
@model SomeNamespace.Models.ChildVM
並重命名您的EditorTemplate
以匹配
視圖/共享/EditorTemplates/ChildVM.cshtml
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.