C# 在MVC4问题中使用RenderAction(actionname,values)
我需要显示实体的一些子对象(C# 在MVC4问题中使用RenderAction(actionname,values),c#,asp.net-mvc,entity-framework,razor,C#,Asp.net Mvc,Entity Framework,Razor,我需要显示实体的一些子对象(项)请求。我发现在一个包含比原始请求实体更多信息的视图中传递请求更好。我将此视图称为RequestInfo,它还包含原始请求Id 然后在MVC视图中,我做了: @model CAPS.RequestInfo ... @Html.RenderAction("Items", new { requestId = Model.Id }) 呈现: public PartialViewResult Items(int requestId) { using (va
项
)请求
。我发现在一个包含比原始请求实体更多信息的视图中传递请求更好。我将此视图称为RequestInfo
,它还包含原始请求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>
@model IEnumerable
@ActionLink(“新建”、“创建”)
@DisplayNameFor(model=>model.Code)
@DisplayNameFor(model=>model.Description)
@Html.DisplayNameFor(model=>model.Qty)
@DisplayNameFor(model=>model.Value)
@DisplayNameFor(model=>model.Type)
@foreach(模型中的var项目){
@DisplayFor(modeleItem=>item.Code)
@DisplayFor(modelItem=>item.Description)
@DisplayFor(modelItem=>item.Qty)
@DisplayFor(modelItem=>item.Value)
@DisplayFor(modelItem=>item.Type)
@ActionLink(“编辑”,“编辑”,新的{id=item.id})|
@ActionLink(“详细信息”,“详细信息”,新的{id=item.id})|
@ActionLink(“删除”,“删除”,新的{id=item.id})
}
但是我在
RenderAction
行上遇到一个编译器错误“不能隐式地将类型'void'转换为'object'”有什么想法吗?调用渲染方法时需要使用以下语法:
@{ Html.RenderAction("Items", new { requestId = Model.Id }); }
不带花括号的@语法
,要求返回类型呈现给页面。为了调用从页面返回void的方法,必须将调用用大括号括起来
请参阅以下链接以获得更深入的解释
有用的替代方案:
@model CAPS.RequestInfo
...
@Html.Action("Items", new { requestId = Model.Id })
此代码返回MvcHtmlString。使用partialview和view结果。不需要{}字符。+1:同样有效的答案和更好的语法。这需要更多的投票:)