[英]Using RenderAction(actionname, values) in MVC4 issue
我需要显示实体Request
一些子对象( Items
)。 而不是请求我发现传递包含比原始请求实体更多信息的视图更好。 这个视图我调用了RequestInfo
,它还包含原始的Requests Id
。
然后在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'转换为'object'”任何想法?
调用Render方法时需要使用此语法:
@{ Html.RenderAction("Items", new { requestId = Model.Id }); }
没有花括号的@syntax
需要一个返回类型,它被呈现给页面。 为了调用从页面返回void的方法,必须用大括号包装调用。
请参阅以下链接以获得更深入的解释。
http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx
有用的替代方案:
@model CAPS.RequestInfo
...
@Html.Action("Items", new { requestId = Model.Id })
此代码返回MvcHtmlString。 使用partialview和查看结果。 不需要{}字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.