spr*_*t12 22 c# asp.net-mvc entity-framework razor
我需要显示Items一个实体的一些子对象()Request.而不是请求我发现传递包含比原始请求实体更多信息的视图更好.我调用此视图RequestInfo,它还包含原始请求Id.
然后在MVC视图中我做了:
@model CAPS.RequestInfo
...
@Html.RenderAction("Items", new { requestId = Model.Id })
Run Code Online (Sandbox Code Playgroud)
渲染 :
public PartialViewResult Items(int requestId)
{
using (var db = new DbContext())
{
var items = db.Items.Where(x => x.Request.Id == requestId);
return PartialView("_Items", items);
}
}
Run Code Online (Sandbox Code Playgroud)
哪个会显示一个通用列表:
@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>
Run Code Online (Sandbox Code Playgroud)
但我得到一个编译器错误的RenderAction行"不能隐含转换类型'void'到'对象'"任何想法?
Jos*_*hua 49
调用Render方法时需要使用此语法:
@{ Html.RenderAction("Items", new { requestId = Model.Id }); }
Run Code Online (Sandbox Code Playgroud)
的@syntax,没有花括号,预计其获取呈现页面返回类型.为了调用从页面返回void的方法,必须用大括号包装调用.
请参阅以下链接以获得更深入的解释.
http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx
小智 15
有用的替代方案:
@model CAPS.RequestInfo
...
@Html.Action("Items", new { requestId = Model.Id })
Run Code Online (Sandbox Code Playgroud)
此代码返回MvcHtmlString.使用partialview和查看结果.不需要{}字符.
| 归档时间: |
|
| 查看次数: |
52843 次 |
| 最近记录: |