Getting error "Child actions are not allowed to perform redirect actions" in MVC


I am using ASP.NET MVC and trying to use HTML.RenderAction to render partial View, inside Main View, but getting error as "Child actions are not allowed to perform redirect actions."

Here is my .cshtml code 

<div class="col-lg-3">
        @{Html.RenderAction("GetLeftMenu", new { category = Model.Category });}
    </div>
    <div class="col-lg-9">
        @Html.Raw(Model.TutorialText)
  </div>
</div>

and Here is my C# controller code

       [AllowAnonymous]
        public ActionResult GetLeftMenu(string category)
        {
            using (var context = new ModelEntities())
            {                           
                  var  List = context.Tuts.Where(a => a.Category == category && a.IsActive == true).Select(a=> new TutorialLeftMenu { Title= a.TutorialTitle, Id= a.Id, URL=a.TutorialURL, Order=Convert.ToInt32(a.TutOrder),MainHeader=a.MainHeader, Category=a.Category }).OrderBy(a=>a.Order).ToList();
                  return PartialView(List);
                
            }
           
        }

how can I resolve this error?


Asked by:- neena
1
: 8503 At:- 11/24/2018 7:12:31 AM
ASP.NET C# asp.net mvc







1 Answers
profileImage Answered by:- Vinnu

You cannot send PartialView like this, as this is not allowed because MVC has already started Rendering the View to the browser (client). So the MVC Frameworks blocks this, because the client already receives data (html). As long as the rendering is in progress you not able to redirect in your child view.

Means:

  • the action corresponding to the URL has been processed and a view result has been returned.
  • ASP.NET MVC's view engine has started rendering the view, possibly sending headers and part of the HTML to the browser.
  • the view engine has come across a RenderAction() call and has started executing the action.
  • the action has tried to return a Redirect HTTP status code to the browser

You can return RedirectToAction instead (if needed)

OR

This can also happen if you have [RequireHttps] attribute on the Controller, and a child action was called from a different controller.

So RequireHttps can cause issue.

1
At:- 11/26/2018 3:08:01 PM Updated at:- 12/1/2022 7:22:27 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use