我需要显示实体 Request
的一些子对象 (Items
)。我发现传递包含比原始请求实体更多信息的 View 而不是请求。我将此 View 称为 RequestInfo
,它还包含原始请求 Id
。
然后在 MVC View 中我做了:
@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
行 "Cannot implicity convert type 'void' to 'object'" 有什么想法吗?
最佳答案
调用 Render 方法时需要使用此语法:
@{ Html.RenderAction("Items", new { requestId = Model.Id }); }
没有大括号的@syntax
需要一个呈现给页面的返回类型。为了从页面调用返回 void 的方法,您必须将调用用花括号括起来。
请参阅以下链接以获得更深入的解释。
http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx
关于c# - 在 MVC4 问题中使用 RenderAction(actionname, values),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14157072/