2013-01-04 9 views
19

エンティティRequestの子オブジェクト(Items)を表示する必要があります。リクエストの代わりに、元のリクエストエンティティより多くの情報を含むビューを渡す方が良いとわかった。私がRequestInfoと呼んでいるこのビューには、元のリクエストIdも含まれています。MVC4でRenderAction(actionname、values)を使用する

はその後MVCビューに私がやった:

@model CAPS.RequestInfo 
...  
@Html.RenderAction("Items", new { requestId = Model.Id }) 

をレンダリングする:

ジェネリックリスト表示していました
public PartialViewResult Items(int requestId) 
{ 
    using (var db = new DbContext()) 
    { 
     var items = db.Items.Where(x => x.Request.Id == requestId); 
     return PartialView("_Items", items); 
    } 
} 

@model IEnumerable<CAPS.Item> 

<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 
<table> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.Code) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Description) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Qty) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Value) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Type) 
     </th> 
     <th></th> 
    </tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.Code) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Description) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Qty) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Value) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Type) 
     </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> 

をしかし、私はコンパイラエラーを取得していますRenderAction"暗黙のうちに型 'void'を 'o'に変換できませんbject '"アイデア?レンダリングメソッドを呼び出すときに

答えて

43

あなたはこの構文を使用する必要があります。

@{ Html.RenderAction("Items", new { requestId = Model.Id }); } 

@syntaxは、中括弧なしで、ページにレンダリングされる戻り値の型を期待しています。ページからvoidを返すメソッドを呼び出すには、中括弧で囲む必要があります。

詳細については、次のリンクをご覧ください。

http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx

14

有用に代わる:

@model CAPS.RequestInfo 
...  
@Html.Action("Items", new { requestId = Model.Id }) 

このコードはMvcHtmlStringを返します。パーシャルビューとビュー結果で動作します。 {}文字は必要ありません。

+1

+1:同様に有効な回答であり、剃刀の構文に適しています。これはより多くの票が必要です:) –

関連する問題