Potrzebuję wyświetlić niektóre obiekty podrzędne (Items
) obiektu Request
. Zamiast Żądania znalazłem lepsze przekazywanie w widoku zawierającym więcej informacji niż oryginalny Podmiot żądający. Ten widok nazywałem RequestInfo
, zawiera on również oryginalne żądania Id
.Korzystanie z funkcji RenderAction (nazwa akcji, wartości) w wydaniu MVC4
Następnie w MVC View zrobiłem:
@model CAPS.RequestInfo
...
@Html.RenderAction("Items", new { requestId = Model.Id })
Aby Render:
public PartialViewResult Items(int requestId)
{
using (var db = new DbContext())
{
var items = db.Items.Where(x => x.Request.Id == requestId);
return PartialView("_Items", items);
}
}
Które wyświetlić listę Generic:
@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>
ale otrzymuję błąd kompilatora na linii RenderAction
"Nie można domyślnie przekształcić typu" void "na" o bject "" Jakieś pomysły?
+1: Równie poprawna odpowiedź i lepiej pasuje do składni brzytwy. To wymaga więcej głosów :) –