[英]Using RenderAction(actionname, values) in MVC4 issue
I need to display some child objects ( Items
) of an entity Request
. 我需要显示实体Request
一些子对象( Items
)。 Instead of Request I found it better to pass in a view that contains more info than the original Request Entity. 而不是请求我发现传递包含比原始请求实体更多信息的视图更好。 This view I called RequestInfo
, it also contains the original Requests Id
. 这个视图我调用了RequestInfo
,它还包含原始的Requests Id
。
Then in the MVC View I did : 然后在MVC视图中我做了:
@model CAPS.RequestInfo
...
@Html.RenderAction("Items", new { requestId = Model.Id })
To 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);
}
}
Which would display a generic list : 哪个会显示一个通用列表:
@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>
But I am getting a compiler error on the RenderAction
line "Cannot implicity convert type 'void' to 'object'" Any ideas? 但是我在RenderAction
行上遇到编译器错误“无法将隐式转换类型'void'转换为'object'”任何想法?
You need to use this syntax when calling the Render methods: 调用Render方法时需要使用此语法:
@{ Html.RenderAction("Items", new { requestId = Model.Id }); }
The @syntax
, without the curly braces, expects a return type which gets rendered to the page. 没有花括号的@syntax
需要一个返回类型,它被呈现给页面。 In order to call a method that returns void from the page, you must wrap the call in curly braces. 为了调用从页面返回void的方法,必须用大括号包装调用。
Please see the following link for a more in-depth explanation. 请参阅以下链接以获得更深入的解释。
http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx
Usefully alternative: 有用的替代方案:
@model CAPS.RequestInfo
...
@Html.Action("Items", new { requestId = Model.Id })
This code returns MvcHtmlString. 此代码返回MvcHtmlString。 Works with partialview and view result. 使用partialview和查看结果。 Doesn't require {} chars. 不需要{}字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.