Tôi cần hiển thị một số đối tượng con (Items
) của một thực thể Request
. Thay vì Yêu cầu, tôi thấy tốt hơn khi chuyển sang chế độ xem có chứa nhiều thông tin hơn Mục yêu cầu ban đầu. Chế độ xem này tôi gọi là RequestInfo
, nó cũng chứa Yêu cầu ban đầu Id
.Sử dụng RenderAction (actionname, values) trong số MVC4
Sau đó, trong MVC Xem tôi đã làm:
@model CAPS.RequestInfo
...
@Html.RenderAction("Items", new { requestId = Model.Id })
Để 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);
}
}
Trong đó sẽ hiển thị một danh sách chung:
@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>
Nhưng tôi nhận được một lỗi biên dịch trên dòng RenderAction
"Không thể thay đổi loại chuyển đổi 'void' thành 'o bject '" Bất kỳ ý tưởng nào?
+1: Câu trả lời hợp lệ và phù hợp hơn với cú pháp Razor. Điều này cần thêm phiếu bầu :) –