Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
That doesn't make any sense. What do you want it to do? –  SLaks Jun 4 '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 4 '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 4 '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 4 '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 4 '13 at 21:44

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
perfect! Thank you! –  Gijs Jun 5 '13 at 1:38

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
share|improve this answer
    
No; partial views do not have layouts. –  SLaks Jun 4 '13 at 21:39
    
But could be set –  Nikola Mitev Jun 4 '13 at 21:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.