2015-10-11 121 views
1

我有2個強類型局部視圖顯示在強類型視圖中。我使用了存儲庫模式。強類型視圖中的多個部分視圖(ASP.NET MVC4)

下面的代碼到父模型

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace BOL 
{ 
    public class HomeMultipleBinder 
    { 
     public IEnumerable<game> g { get; set; } 
     public IEnumerable<team> t { get; set; } 
    } 
} 

我這裏還有S上的控制器:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using BOL; 

namespace TeamBuildingCompetition.Areas.Common.Controllers 
{ 
    public class gHomeController : BaseCommonController 
    { 
     // GET: Common/gHome 
     public ActionResult Index() 
     { 
      var model = new HomeMultipleBinder(); 
      model.g = objBs.gameBs.GetALL(); 
      model.t = objBs.teamBs.GetALL(); 
      return View(model); 
     } 
    } 
} 

和這裏的視圖:

@model BOL.HomeMultipleBinder 
@{ 
    ViewBag.Title = "Index"; 
    Layout = "~/Views/Shared/_Layout_new.cshtml"; 
} 

<h2>Index</h2> 

@{ 
    @Html.RenderPartial("_gameView",Model.g); 
    @Html.RenderPartial("_teamView",Model.t); 
} 

和下面是各自的局部視圖:

@model IEnumerable<BOL.game> 

    <table class="table"> 
     <tr> 
      <th> 
       @Html.DisplayNameFor(model => model.gameName) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.description) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.content) 
      </th> 
     </tr> 

    @foreach (var item in Model) { 
     <tr> 
      <td> 
       @Html.DisplayFor(modelItem => item.gameName) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.description) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.content) 
      </td> 
     </tr> 
    } 

    </table> 

@model IEnumerable<BOL.team> 

<table class="table"> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.teamName) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.teamPicture) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.description) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.content) 
     </th> 
    </tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.teamName) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.teamPicture) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.description) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.content) 
     </td> 
    </tr> 
} 

</table> 

我有以下編譯器錯誤「消息:CS1502:爲了獲得最佳重載方法匹配 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' 具有一些無效參數」

+1

它必須是大寫M - 'Html.RenderPartial( 「...」,Model.g)'和'你指數()'方法應該返回'查看()','不PartialView ()' –

+1

NullReferenceError在哪裏?在model.g和model.t模型的intellisense下劃線?你可以嘗試使用模型而不是模型?編輯:韋爾普斯蒂芬的在線,不妨上牀睡覺!他的解決方案將工作 –

+0

@StephenMuecke:我仍然有錯誤...編譯器錯誤消息:CS1061:'System.Collections.Generic.IEnumerable '不包含'g'的定義並且沒有擴展方法'g '可以找到類型'System.Collections.Generic.IEnumerable '的第一個參數(你是否缺少using指令或程序集引用?) – Guzzyman

回答

0

我不認爲你可以在你的看法寫這篇文章:

@Html.DisplayNameFor(model => model.content) 

您的模式類型爲IEnumerable的

難道這不是給予任何錯誤!

this might help

+0

Stephen Muecke在私下聊天中解決了這個問題。我只是刪除Html.RenderPartial(「_ gameView」,Model.g)之前的標誌;在我的問題上面,它的工作。 – Guzzyman