cannot implicitly convert type void to object. .NET MVC PartialViewResult
Asked Answered
G

4

101

I have the following controller action:

[ChildActionOnly]
public virtual PartialViewResult ListActions(int id)
{
    var actions = meetingActionRepository.GetAllMeetingActions(id);

    return PartialView(actions);
}

And the following action link (using t4MVC and the razor syntax)

<p>
   @Html.RenderAction(MVC.MeetingActions.ListActions(Model.MeetingId))
</p>

However this gives me the error:

cannot implicitly convert type void to object

As far as i can tell the controller action is ok, so what could be giving me this error?

Greenwich answered 7/2, 2011 at 22:50 Comment(0)
L
137

Like this:

<p>
    @Html.Action(MVC.MeetingActions.ListActions(Model.MeetingId))
</p>

or if you insist on RenderAction like this:

<p>
    @{Html.RenderAction(MVC.MeetingActions.ListActions(Model.MeetingId));}
</p>

Personally I prefer the first, makes fewer keystrokes.

Lucrece answered 7/2, 2011 at 22:50 Comment(3)
Thank you. Funky syntax there. I assume that's the razor way of doing <%html instead of <%=Html ? What's the difference between Action and Renderaction anyway. Phil Haack says RenderAction is more efficient?Greenwich
@Doozer1979, yeah, exactly, that's the Razor way.Lucrece
This also works : {Html.RenderAction(MVC.MeetingActions.ListActions(Model.MeetingId));}, so using @ is not necessary.Khamsin
H
50

I had the same issue. What worked for me is to encapsulate the expression it in curly brackets.

@{Html.RenderPartial("viewName", Model);}

Hope answered 8/12, 2016 at 14:13 Comment(3)
This worked for me, however I have no idea why adding curling brackets would work. Can you elaborate on that?Theoretics
@BradThiessen It works because ASP.NET MVC is quite frankly a shoddy framework full of glaring holes and poor design decisions. I just came across this too. Who knows why, but my patience is wearing thin with MVC.Kiki
@BradThiessen RenderPartial() is a void, using @ you say the Razor to print it which is not possible as it doesn't produce any output content!Frisky
I
45

Html.Partial should work as well :)

@Html.Partial("View", Model);
Ihram answered 20/11, 2013 at 18:3 Comment(0)
E
6

Difference between Html.RenderAction and Html.Action

Different things for different purposes. Check out the above link.

Essene answered 4/3, 2012 at 7:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.