c# - 在 MVC4 问题中使用 RenderAction(actionname, values)

标签 c# asp.net-mvc entity-framework razor

我需要显示实体 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/

相关文章:

c# - ASP.NET MVC AuthorizeAttribute 将值传递给 Action 方法?

c# - 未经授权暴露hangfire

jquery - 如何使用 jQuery DataTables 发布整个表的数据

c# - 是否可以注册多点文件扩展名?

c# - 手动键入 sql 以映射到 c# 对象,就像 .Include ("") 方法

entity-framework - 解决 EF4 中的实体与我们的数据库标准之间的命名约定冲突?

c# - 包括导航属性

c# - 将变量分配给 lambda 表达式的结果

c# - 在给定属性的列表中获取项目的索引

c# - AzureStorage 库 v2 中的 "tableClient.CreateTableIfNotExist"在哪里?