2017-05-21 14 views
0

このトピックに関する多くの質問と回答がありますが、これはうまく動作しません。MVC 5 RenderPageまたはRenderPartialを正常に動作させることができません

私は2つのビューを持っています。 1つは親を表し、もう1つは子レコードを表します。すべてが読み取り専用なので、ビューは非常にシンプルです。ここで

は私の親ビューは次のとおりです。ここで

@model IEnumerable<WebApplication1.vw_CurrentDump> 

@{ 
    ViewBag.Title = "CurrentDump"; 
    Layout = "~/Views/Shared/_MyLayout.cshtml"; 
} 
@section head{ 
    <link href="@Url.Content("~/Content/css/style.css")" rel="stylesheet" type="text/css" } /> 
} 
<style> 
    .headerTest { 
     text-align: center; 
     width: 100%; 
    } 
</style> 

<h2 class="headerTest">Current Dump</h2> 

<table class="aTable"> 
    <tr> 
     <th> 
      Dump # 
     </th> 
     <th> 
      Units 
     </th> 
     <th> 
      Start 
     </th> 
     <th> 
      Cars 
     </th> 
     <th> 
      Weight 
     </th> 
     <th> 
      Cars Dumped 
     </th> 
     <th> 
      Tons Dumped 
     </th> 
     <th> 
      Destination 
     </th> 
    </tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.DumpUnitId) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Units) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.StartTime) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Cars) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Weight) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.CarsDumped) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.TonsDumped) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Desintation) 
     </td> 
    </tr> 
} 

</table> 
@{ Html.RenderPartial("CurrentDumpCars"); } 

は、子レコードのための図である。

@model IEnumerable<WebApplication1.vw_CurrentDumpCars> 

@{ 
    Layout = null; 
} 
<table class="aTable"> 
    <tr> 
     <th> 
      Car Number 
     </th> 
     <th> 
      Date Dumped 
     </th> 
     <th> 
      Weight 
     </th> 
     <th> 
      Empty Track 
     </th> 
    </tr> 

    @foreach (var item in Model) 
    { 
     <tr> 
      <td> 
       @Html.DisplayFor(modelItem => item.CarNumber) 
      </td> 
      <td> 
       @if (item.DateDumped.HasValue) 
       { 
        @item.DateDumped.Value.ToString("MM/dd/yyyy HH:mm") 
       } 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.Weight) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.EmptyTrackNumber) 
      </td> 
     </tr> 
    } 

</table> 

(示すように)私は@RenderPageとれるrenderPartialを試してみました。どちらも、同じエラーを返す:

System.InvalidOperationException: 'The model item passed into the dictionary is of type 'System.Collections.Generic.List 1[WebApplication1.vw_CurrentDump]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1[WebApplication1.vw_CurrentDumpCars]'.'

それは私がれるrenderPartial方法にvw_CurrentDumpに渡していることを私に言っているように見えるが、それはどんな意味がありません。どちらのビューもEntity Framework 6ライブラリにあります。私が読んだすべては、これがうまくいくはずだと私に伝えます。

答えて

1

CurrentDumpCars@model IEnumerable<WebApplication1.vw_CurrentDumpCars>モデルですが、あなたの@{ Html.RenderPartial("CurrentDumpCars"); }は値も正しい値も渡しませんでした。 CurrentDumpCarsは別のアクションを持っているならば、むしろ@{ Html.RenderPartial("CurrentDumpCars"); }より@{ Html.RenderAction("CurrentDumpCars"); }を試すか@{ Html.RenderPartial("CurrentDumpCars"); }

例にIEnumerable<WebApplication1.vw_CurrentDumpCars>モデルを渡す:

@{ 
var items= new IEnumerable<WebApplication1.vw_CurrentDumpCars>()//your data 
} 

@{ Html.RenderPartial("CurrentDumpCars",items); } 
+0

ファンタスティック!どうもありがとうございます。私はMVCについて学ぶことがたくさんある。私はあまりにも長い間WPFをやってきました。 RenderActionがトリックを行いました。 –

+0

@ MarkBonafe大歓迎です。 – Ashiquzzaman

関連する問題