7

I am converting a WebForms application to Razor and everything works fine except when I try to use Html.RenderAction. Whenever I call this, I get a StackOverflowException. Does anyone have an idea on what might be causing this?

The template for my action looks like this:

@model dynamic   

should be rendering this

In my _Layout.cshtml file I render the action like this:

@{Html.RenderAction("MyPartialAction");}

My _ViewStart.cshtml file looks as follows:

@{
    this.Layout = "~/Views/Shared/_Layout.cshtml";
}

1 Answer 1

19

The problem is that your template for your action does not define a Layout to be used. Therefore, it automatically gets the one specified in the _ViewStart.cshtml file. This will in effect cause the _Layout.cshtml file to be nested within itself ad infinitum. Hence the StackOverflowException. The solution is simple. Set the Layout within your action template to null:

@model dynamic
@{
   Layout = null;
}
should be rendering this

Now the template won't request to be embedded in a layout file and everything works fine.

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

1 Comment

Or use the correct method and call Html.RenderPartial().

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.