本文介绍了在MVC4使用问题的RenderAction(actionname,价值观)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示一个实体的一些子对象(项目请求。相反,请求我发现它更好地在包含比原来的请求实体的详细信息视图通过。这个观点我叫 RequestInfo ,它也包含原始请求编号

I need to display some child objects (Items) of an entity Request. 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.

然后在MVC视图我所做的:

Then in the MVC View I did :

@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);
    }
}

这将显示一个泛型列表:

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>

但我得到的的RenderAction 行编译器错误的不能含蓄转换类型'无效'到'对象'任何想法?

But I am getting a compiler error on the RenderAction line "Cannot implicity convert type 'void' to 'object'" Any ideas?

推荐答案

您需要使用这个语法调用渲染方法时:

You need to use this syntax when calling the Render methods:

@{ Html.RenderAction("Items", new { requestId = Model.Id }); }

@语法,没有花括号,预计它被呈现在页面返回类型。为了调用从页面返回void的方法,就必须包装在大括号的电话。

The @syntax, without the curly braces, expects a return type which gets rendered to the page. In order to call a method that returns void from the page, you must wrap the call in curly braces.

请参阅更深入的解释下面的链接。

Please see the following link for a more in-depth explanation.

http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx

这篇关于在MVC4使用问题的RenderAction(actionname,价值观)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 07:57