2015-11-02 74 views
0

我有一個不顯示數據的局部視圖。這是一個簡單的版本。這是我的模特。父母和孩子的記錄部分視圖不顯示數據

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

子窗體應顯示的孩子,但不

@model IEnumerable<Partial.Models.Child> 

@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 

<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 
<table class="table"> 
<tr> 
    <th> 
     @Html.DisplayNameFor(model => model.Description) 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.Parent.Description) 
    </th> 
    <th></th> 
</tr> 

@foreach (var item in Model) { 
<tr> 
    <td> 
     @Html.DisplayFor(modelItem => item.Description) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.Parent.Description) 
    </td> 
    <td> 
     @Html.ActionLink("Edit", "Edit", new { id=item.ID }) | 
     @Html.ActionLink("Details", "Details", new { id=item.ID }) | 
     @Html.ActionLink("Delete", "Delete", new { id=item.ID }) 
    </td> 
</tr> 
} 

</table> 
+0

更多關於這個問題的上下文將有助於搞清楚這個問題出在哪裏。 – Greg

+0

你還想看什麼? –

回答

0
Please Try Code 

刪除代碼

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

替換代碼

@ {Html.RenderActi on(「Index」,「Children」);}

Sysntext For : 


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