簡體   English   中英

在MVC4問題中使用RenderAction(actionname,values)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM