0

I'm trying to render from controller either PartialView or View depending on condition coming from outside. Web-site fails on posting data with StackOverflowException.

Controller code:

    public ActionResult Login(bool partial = false)
    {
        if (partial)
        {
            ViewBag.Partial = true;
            return PartialView();
        }
        return View();
    }

    [HttpPost]
    public ActionResult Login(UserViewModel userViewModel)
    {
        if (!ModelState.IsValid)
            return View(userViewModel);
        // some authrorization staff
    }

Login.cshtml:

    @using SK.DDP.ImageGallery.Helpers
    @model SK.DDP.ViewModels.UserViewModel

    @{
        ViewBag.Title = "Login";
        if (ViewBag.Partial != null)
        {
            Layout = string.Empty;
        }
    }

    @*Some form staff*@

AuthorizationInfo.cshtml:

    @{
        Layout = string.Empty;
    }

    @{ Html.RenderAction("Login", "Authorization"); }

Template:

    @*Some dif for form requested by menu*@
    @{ Html.RenderAction("AuthorizationInfo", "Authorization"); }

I have a web-site with login page and login popup window appearing when user clicks on menu, so i wanted to reuse the same action of controller and code and app is continuing to fail with stackoverflow exception.

Thanks.

Sunil Kumar
  • 3,142
  • 1
  • 19
  • 33
Siarhei Kuchuk
  • 5,296
  • 1
  • 28
  • 31
  • 1
    How is `AuthorizationInfo.cshtml` and Template related to the question? And you do not need `if (ViewBag.Partial != null)` in the view (your returning a PartialView which will not use a layout) –  Sep 15 '16 at 23:39
  • 1
    You don't need to set the Layout = string.Empty if the returned actionresult was partial, can you confirm this? – Monah Sep 15 '16 at 23:43
  • 1
    Stack overflow exception ? Look for some recursive calls. May be a action method keep calling it self ? – Shyju Sep 15 '16 at 23:50
  • 1
    Since `StackOverflowException` cannot be catched, try to set breakpoints on any suspected code lines, especially on loops, recursions or methods that consume large amount of memory. Refer to this post for mitigating/preventing SO: http://stackoverflow.com/questions/206820/how-do-i-prevent-and-or-handle-a-stackoverflowexception – Tetsuya Yamamoto Sep 16 '16 at 08:27

1 Answers1

0

Seems its a bug in a Razor engine.

I workarounded it.

AuthorizationInfo.cshtml

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

AuthorizationController.cs

public ActionResult Login()
{
    return View();
}

public ActionResult LoginPartial()
{
    ViewBag.Partial = true;
    return PartialView("Login");
}

Now Post of forms does not generate overflows with templates applied recursively.

Siarhei Kuchuk
  • 5,296
  • 1
  • 28
  • 31