Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using RenderAction(actionname, values) in MVC4 issue

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.

Then in the MVC View I did :

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

To Render :

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>

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

like image 645
sprocket12 Avatar asked Jan 04 '13 12:01

sprocket12


People also ask

Which of the following differentiates action from RenderAction?

The difference between the two is that Html. RenderAction will render the result directly to the Response (which is more efficient if the action returns a large amount of HTML) whereas Html. Action returns a string with the result.

What is the difference between RenderPartial and RenderAction?

RenderPartial is used to display a reusable part of within the same controller and RenderAction render an action from any controller. They both render the Html and doesn't provide a String for output.

What is RenderAction in MVC?

RenderAction(HtmlHelper, String) Invokes the specified child action method and renders the result inline in the parent view. RenderAction(HtmlHelper, String, Object) Invokes the specified child action method using the specified parameters and renders the result inline in the parent view.

What is the difference between HTML partial vs HTML RenderPartial & HTML action vs HTML RenderAction?

Render vs Action partialRenderPartial will render the view on the same controller. But RenderAction will execute the action method , then builds a model and returns a view result.


2 Answers

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

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

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

like image 166
Joshua Avatar answered Nov 17 '22 08:11

Joshua


Usefully alternative:

@model CAPS.RequestInfo
...    
@Html.Action("Items", new { requestId = Model.Id })

This code returns MvcHtmlString. Works with partialview and view result. Doesn't require {} chars.

like image 40
gelistirici Avatar answered Nov 17 '22 07:11

gelistirici