部分视图不显示数据

时间:2015-11-02 23:44:25

标签: view partial

我有一个不显示数据的局部视图。这是一个简单的版本。这是我的模特。父母和子女记录

git checkout master
git pull origin master
git checkout featurebranch
git merge master

以下是我的控制器,只涉及索引和编辑表单。

namespace Partial.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Parent
     {
         [System.Diagnostics.CodeAnalysis.SuppressMessage   ("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Parent()
        {
            this.Children = new HashSet<Child>();
        }

        public int ID { get; set; }
        public string Description { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Child> Children { get; set; }
    }
}

namespace Partial.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Child
    {
        public int ID { get; set; }
        public Nullable<int> ParentID { get; set; }
        public string Description { get; set; }

        public virtual Parent Parent { get; set; }
    }
}

这是我的看法。父表格是编辑表格。哪个应显示子索引表单

namespace Partial.Controllers
{
    public class ParentsController : Controller
    {
        private PartialEntities db = new PartialEntities();

        // GET: Parents
        public ActionResult Index()
        {
            return View(db.Parents.ToList());
        }

namespace Partial.Controllers
{
    public class ChildrenController : Controller
{
    private PartialEntities db = new PartialEntities();

    // GET: Children
    public ActionResult Index()
    {
        var children = db.Children.Include(c => c.Parent);
        return View(children.ToList());

    }

应该显示孩子但不显示的儿童表格

@model Partial.Models.Parent

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>


@using (Html.BeginForm())
{

    <div class="form-horizontal">
    <h4>Parent</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.ID)

    <div class="form-group">
        @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
</div>
}
<table class="table">
<tr>
    <td>
        Html.Partial("~/Views/Children/Index.cshtml", new List<Partial.Models.Child> ());
    </td>        
</tr>
</table>

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

1 个答案:

答案 0 :(得分:0)

Please Try Code

删除代码

 Html.Partial("~/Views/Children/Index.cshtml", new List<Partial.Models.Child> ());

替换代码

@ {Html.RenderAction( “索引”, “儿童”);}

Sysntext For :


 @Html.RenderAction("Action name","Controller name",Route values)