1

I wrote a piece of code that returns a random string sponsorname from a list of sponsors. This sponsorname should be visible at each page, so I call the RandomSponsor method in the shared _layout view. This RandomSponsor method is based in the HomeController and has it's own view containing only a Html.Action

And at that Html.Action the program returns an error:

System.StackOverflowException {Cannot evaluate expression because the current thread is in a stack overflow state.}

This is my RandomSponsor method:

    [HttpGet]   
[ChildActionOnly]
public ActionResult RandomSponsor()
{
    var model = service.getRandomSponsor();
    return PartialView("RandomSponsor", model);
}

RandomSponsor.cshtml, where the programs stops

@Html.Action("RandomSponsor")

And my call in the shared layout page _Layout.cshtml:

@Html.Action("RandomSponsor", "Home")

While i'm debugging i noticed that the RandomSponsor method goes to it's view, but because my Html.Action requests the function again, it's stuck in a loop. I think I give the wrong parameter to the Html.Action in the RandomSponsor.cshtml view, but I dont know what is the correct one.

Does anyone had a similar problem or knows how to fix this error, i'm all ears.

Regards

Gijs
  • 885
  • 2
  • 13
  • 22
  • That doesn't make any sense. What do you want it to do? – SLaks Jun 04 '13 at 21:30
  • @SLaks, the final result should be a random sponsorname on each page of my website. With this code I try to render the RandomSponsor.cshtml view on the shared layout with a partialView. – Gijs Jun 04 '13 at 21:34
  • Need more information. Does the exception originate from the `getRandomSponsor()` method? What is it's implementation? Can you point out the last line of your code noted in the stack trace? For example if it's thrown from `getRandomSponsor()` there will be a line for `getRandomSponsor()` and also one for `RandomSponsor()` then probably several lines in framework code. – evanmcdonnal Jun 04 '13 at 21:35
  • @Gijs: Your view still doesn't make any sense. Why is the view for that action invoking the action again? (with the same view) – SLaks Jun 04 '13 at 21:40
  • @evanmcdonnal I checked in the HomeController and there is the right sponsorName, so the getRandomSponsor() method works fine. And the error Details posted in the original question is really the only info i get from visual studio. – Gijs Jun 04 '13 at 21:44
  • @SLaks this is the first time i use PartialViews and searched the web for usefull info about my problem but didn't really found anything so i figured out the partialView needed a viewpage. I tried to base this code on the info i found here http://stackoverflow.com/questions/14011026/the-controller-for-path-was-not-found-or-does-not-implement-icontroller?lq=1 – Gijs Jun 04 '13 at 21:49
  • I think adequate answers have been provided but on a side not you should find the stack trace in visual studio. There's a debugger window called Call Stack that will provide you a lot more information when an exception is thrown while running with debug. – evanmcdonnal Jun 04 '13 at 22:11

2 Answers2

2

The problem is that it seems for RandomSponsor partial view you have set _Layout.chtml as layout,

enter image description here

so you have this scenario:

_Layout.chtml calls RandomSponsor, RandomSponsor first load it's layout _Layout.chtml,
_Layout.chtml calls RandomSponsor, RandomSponsor first load it's layout _Layout.chtml....till stackoverflow
Nick
  • 4,192
  • 1
  • 19
  • 30
2

You need to put the actual HTML that you want to child action to render in its view.

It doesn't make sense to have the view recursively render its own action.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964