Asp.Net MVC 3 - @Html.Action won't render/return any HTML
Asked Answered
B

4

7

I've been moving a fairly new project from ViewPages to Razor today, and all seems to be going well. Except I'm trying to use Html.Action to render a user control and it won't render anything.

So I have a Shared/_Layout.cshtml file which is referenced in Home/Index.cshtml

Index.cshtml has the following:

<article>

@Html.Action("LatestBlogsMainPanelWidget", "Blogs")

...

</article>

I've put traps in the BlogsController, so I know that's being requested. I also know that a model is being returned, that the LatestBlogsMainPanelWidget is being found by the view engine, and even some dummy Razor syntax code is being run: @{ var s = "hello"; }

but the plain html in this file isn't making it to the browser. I've tried other (previously working) partials too and they won't appear either (view source on the page confirms it's not there).

I've also tried substituting for @{ Html.RenderAction(...); } without success. HTML either side of the @Html.Action is appearing, so I know Index.cshtml is displaying properly.

Even more strangely the _Layout file also has Html.Action commands and they do appear fine.

I'm really not sure what else to check, or how to confirm that the pipeline is getting the HTML. Can anyone help at all?

Thanks!

Breathe answered 10/12, 2010 at 18:46 Comment(1)
Is the view for that action located in the proper place? Nevermind, I just saw the one line I was looking for.Speech
B
1

The brain is a funny thing, and despite spending several hours on this yesterday, it took my dog waking me up in the middle of the night for a wee for my subconscious to stumble upon the answer.

If this had anyone else stumped, I'm not surprised. I hadn't mentioned because it hadn't dawned on me that I was using a partial-level caching system similar to one designed by Steve Sanderson. It suddenly struck me that this could be the cause, since to the best of my knowledge Razor pages go through far less pipeline processing than WebForm pages. The caching filter is probably not doing what it needs to do, or at the right time.

I've confirmed that commenting out the OutputCache filter on the Actions in question has fixed the problem.

I've no idea if this issue is true of the page-level caching as it's not something I find useful.

Breathe answered 11/12, 2010 at 6:38 Comment(1)
Curiouser and curiouser. I've just installed RC2 of MVC 3 and the partial-level caching is working again, and all of the html.actions are showing up again. Well, sorry for the bother!Breathe
P
8

Put a Layout = null on the partial view and it will work fine.

Pyrostat answered 21/1, 2011 at 19:50 Comment(2)
exactly what I needed. This was useful for my action decorated with the [ChildActionOnly] attribute.Tamarind
actually, I just learned that instead of adding that in, (in my case) I just needed to switch return View to return PartialView.Tamarind
W
6

Try this:

@{Html.RenderAction("LatestBlogsMainPanelWidget", "Blogs");}

Weathered answered 16/12, 2010 at 2:23 Comment(1)
You can see in the original question that I had tried that. Thanks anyway.Breathe
B
1

The brain is a funny thing, and despite spending several hours on this yesterday, it took my dog waking me up in the middle of the night for a wee for my subconscious to stumble upon the answer.

If this had anyone else stumped, I'm not surprised. I hadn't mentioned because it hadn't dawned on me that I was using a partial-level caching system similar to one designed by Steve Sanderson. It suddenly struck me that this could be the cause, since to the best of my knowledge Razor pages go through far less pipeline processing than WebForm pages. The caching filter is probably not doing what it needs to do, or at the right time.

I've confirmed that commenting out the OutputCache filter on the Actions in question has fixed the problem.

I've no idea if this issue is true of the page-level caching as it's not something I find useful.

Breathe answered 11/12, 2010 at 6:38 Comment(1)
Curiouser and curiouser. I've just installed RC2 of MVC 3 and the partial-level caching is working again, and all of the html.actions are showing up again. Well, sorry for the bother!Breathe
C
1

While searching solutions for this issue, I have find out three measure issues for not proper rendering of Html.Action and Html.RenderAction. Please verify have you done below things properly or not.

  1. In your PartialView or View you have defined @{Layout = null;}.
  2. Use return PartialView instead of View .
  3. Decorate your action with [ChildActionOnly] attributes.

I hope by applying above all steps you can solve your issues.

Cade answered 18/9, 2015 at 6:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.