本文介绍了MVC部分视图不从EditorTemplates呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVC 5 / EF 6.1处理带有父母(一对多))关系的表单。
我试图通过部分视图呈现孩子,不会渲染或给我任何错误(在创建一个新的表单)。
我尝试运行调试器,我没有看到任何错误(我确定有)。



到目前为止,这是我所拥有的: / p>

模型

  public class Parent 
{

public int ParentID {get;组; }

public string FirstName {get;组; }
public string LastName {get;组; }

public virtual ICollection< Child>孩子{get;组; }

}
public class Child
{
public int ChildID {get;组; }
public int ParentID {get;组; }

public string Name {get;组; }
public string DOB {get;组; }
public string Address {get;组; }

public virtual Parent Parent {get;组; }
}

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;组; }
public int ChildID {get;组; }

public string FirstName {get;组; }
public string LastName {get;组; }

public string Name {get;组; }
public string DOB {get;组; }
public string Address {get;组; }

public IList< ChildVM>孩子{get;组; }

}

public class ChildVM
{
public int ChildID {get;组; }
public int ParentID {get;组; }

public string Name {get;组; }
public string DOB {get;组; }
public string Address {get;组; }
}

查看(通过脚手架创建:索引,详细信息,编辑,创建,删除)

  @model SomeNamespace.ViewModels.ParentVM 
@ {ViewBag.Title =Create;}

< h2>创建< / h2>

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

< div class =form-horizo​​ntal >
< h4>父< / 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 ,,新的{@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 ,,新的{@class =text-danger})
< / div>
< / div>


< table border =1>
< tr>
< th> Name< / th>
  DOB< / th>
< th>地址< / th>
< / tr>
@ Html.EditorFor(x => x.Children)
< / table>


< div class =form-group>
< div class =col-md-offset-2 col-md-10>
< input type =submitvalue =Createclass =btn btn-default/>
< / div>
< / div>
< / div>}

< div> @ Html.ActionLink(返回列表,索引)< / div>
@section脚本{
@ 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>


Controller(ParentsController.cs)

  // GET:Parents / Create 
public ActionResult Create()
{

// var viewModel = new ParentVM
// {
// Children =
// new List< ChildVM>()
// {
/ / new ChildVM(){Name =1,DOB =1,Address =1},
//新的ChildVM(){Name =1,DOB =1 1},
// new ChildVM(){Name =1,DOB =1,Address =1}
//}
//};

return View(new ParentVM());
// return View(viewModel);
}

// POST:父母/创建
//为了防止过多的攻击,请启用要绑定的特定属性,对于
//更多详细信息,请参见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>
  DOB< / th>
< th>地址< / th>
< / tr>

< / table>

也许它不喜欢类型?我以为你可以在部分视图中使用模型(editorTemplates)?

解决方案

ParentVM 类包含一个属性 public IList< ChildVM>孩子{get;组; } ,但您的 EditorTemplate 用于 @model SomeNamespace.Models.Child 。将其更改为

  @model SomeNamespace.Models.ChildVM 

并重命名您的 EditorTemplate 以匹配


I am working on a form with a parent child (one to many) relationship using 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)

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

// 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)??

解决方案

You ParentVM class contains a property public IList<ChildVM> Children { get; set; } but your EditorTemplate is for @model SomeNamespace.Models.Child. Change it to

@model SomeNamespace.Models.ChildVM

And rename your EditorTemplate to match

这篇关于MVC部分视图不从EditorTemplates呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 23:20