简体   繁体   English

无法从EditorTemplates渲染MVC部分视图

[英]MVC Partial View not rendering from an EditorTemplates

I am working on a form with a parent child (one to many) relationship using MVC 5 / EF 6.1. 我正在使用MVC 5 / EF 6.1与父母(一对多)关系建立表单。 I am trying to render the childs via a partial view that will not render or give me any errors (on creating a new form). 我正在尝试通过局部视图来呈现子级,该局部视图不会呈现或给我任何错误(在创建新表单时)。 I tried running the debugger and I did not see anything wrong (I'm sure there is though). 我尝试运行调试器,但没有发现任何错误(我确定有问题)。

So far this is what I have: 到目前为止,这就是我所拥有的:

Models 楷模

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

ViewModels 视图模型

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

View (created via scaffolding: Index, Details, Edit, Create, Delete) 视图(通过脚手架创建:索引,详细信息,编辑,创建,删除)

@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")}

Partial View (Views/Shared/EditorTemplates/Child.cshtml) 局部视图(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>

Controller (ParentsController.cs) 控制器(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);
    }

This is what I see in the view source: 这是我在视图源中看到的:

<table border="1">
        <tr>
            <th>Name</th>
            <th>DOB</th>
            <th>Address</th>
        </tr>

    </table>

Maybe it does not like the type? 也许它不喜欢这种类型? I thought you were able to use a model in a partial view (editorTemplates)?? 我以为您可以在部分视图中使用模型(editorTemplates)?

You ParentVM class contains a property public IList<ChildVM> Children { get; set; } 您的ParentVM类包含一个属性public IList<ChildVM> Children { get; set; } public IList<ChildVM> Children { get; set; } public IList<ChildVM> Children { get; set; } but your EditorTemplate is for @model SomeNamespace.Models.Child . public IList<ChildVM> Children { get; set; }但您的EditorTemplate用于@model SomeNamespace.Models.Child Change it to 更改为

@model SomeNamespace.Models.ChildVM

And rename your EditorTemplate to match 并重命名您的EditorTemplate以匹配

Views/Shared/EditorTemplates/ChildVM.cshtml 视图/共享/EditorTemplates/ChildVM.cshtml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM