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

7
  • That doesn't make any sense. What do you want it to do? Commented Jun 4, 2013 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. Commented Jun 4, 2013 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. Commented Jun 4, 2013 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) Commented Jun 4, 2013 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. Commented Jun 4, 2013 at 21:44

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

No; partial views do not have layouts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.