1

I'm trying to load my partial view with some data from database, but I'm getting following issue when I run the application:

Child actions are not allowed to perform redirect actions.

I don't know why this is happening because I'm pretty new with MVC technology.

Here is my PartialViewResult method in a controller:

public PartialViewResult UnReadEmails()
{
   if (User.Id != null)
   {
      List<Emails> resultList = EmailController.GetUnreadEmailsByUserId(User.Id);
       return PartialView("~/Views/Emails/_UnReadEmails.cshtml", resultList);
   }
   return PartialView("Error, not found!");
}

And here is my partialview itself, it is called _UnReadEmails (as you can see I'm displaying here info about sender and email body), PartialView is retrieving list of Emails that I'm sending to from my Controller

@model IEnumerable<Emails>

foreach (var item in Model)
{
    <li>
        <a>
            <span>
               <span>@item.EmailSender</span>
               <span class="email">
                     @item.Body;
               </span>
        </a>
    </li>
}

After I tried to load my partial view on this way:

@Html.Action("UnreadEmails", "Message")

I started to receive following issue that I mentioned in my Title,

I already tried few things to solve this like changing @Html.Action("UnreadEmails", "Message") to @Url.Action("UnreadEmails", "Message") etc etc but that didn't solve my issue.

EDIT: It allways breaks on this line (on view) :

@Html.Action("UnreadEmails", "Message")

It never goes into code behind.. enter image description here

After Chris suggestion I added [AllowAnonymous] on the top of the method:

[AllowAnonymous]
public PartialViewResult UnReadEmails()
{
   if (User.Id != null)
   {
      List<Emails> resultList = EmailController.GetUnreadEmailsByUserId(User.Id);
       return PartialView("~/Views/Emails/_UnReadEmails.cshtml", resultList);
   }
   return PartialView("Error, not found!");
}

EDIT EDIT EDIT:

Interesting fact is that whatever I wrote in my Controller's method and even if I comment all code, it will still break on a View, that means it will never came into a Controller's method. I put breakpoing there at the begining of the UnReadEmails method and it was never hitted, it allways breaks on a View!

Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102
  • Side note: `PartialView("Error, not found!")` makes no sense (unless you really have view named that way)... Please debug code and see what exact line causes the issues and [edit] post with that information. – Alexei Levenkov May 31 '17 at 15:01
  • @AlexeiLevenkov I debugged allready and it allways breaks here : @Url.Action("UnreadEmails", "Message"), check for my edit – Roxy'Pro May 31 '17 at 15:04
  • I think it's @Html.Partial instead of action. It's been a while since I did MVC though – PeteG May 31 '17 at 15:05
  • @Peteg I've tried it before and it didn't work :/ – Roxy'Pro May 31 '17 at 15:06

4 Answers4

2

The error is pretty explicit. Child actions cannot issue redirects. This is because, while child actions look like regular actions, they are in fact rendered in a separate process and cannot modify the actual response (which a redirect would require).

In your actual child action code, you're not returning a redirect, so that means you must have an action filter being applied to the child action that is issuing the redirect. In particular, you should avoid using things like Authorize or RequireHttps on child actions, as those work by using redirects, which again, a child action cannot do. If the child action is in a controller that is decorated with Authorize, it should be marked with AllowAnonymous.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • on the top of the class (controller) is [BaseAuthorize], so by following your answer I put [AllowAnonymous] above my PartialViewResult method but I'm still receiving same error. check for my edit, and thanks for your help mate.. – Roxy'Pro May 31 '17 at 16:45
  • Damn still stuggling with this issue :( – Roxy'Pro Jun 01 '17 at 09:56
  • it's all about View, doesn't matter what I wrote in my controller, it will still break there on a View, I tried commenting out everything that's containted in my Controller's method and still I'm receiving same issue on a View. – Roxy'Pro Jun 01 '17 at 15:28
1

Usually it happens when child Action has errors, because exception gets thrown and MVC trying redirect user to error page. In your case it is trying to find view named "Error, not found!", which probably you don't have. Try run Action as itself first, and check if logic works. Then put your view _UnreadEmails.cshtml in Manage Controller Views folder, and modify the code:

public PartialViewResult UnReadEmails()
{
   if (User.Id != null)
   {
      List<Emails> resultList = EmailController.GetUnreadEmailsByUserId(User.Id);
       return PartialView("_UnReadEmails", resultList);
   }
   return PartialView("_UnReadEmails" new List<Emails>());
}

Sometimes empty List means that nothing was found. OR:

public ActionResult UnReadEmails()
{
   if (User.Id != null)
   {
      List<Emails> resultList = EmailController.GetUnreadEmailsByUserId(User.Id);
       return PartialView("_UnReadEmails", resultList);
   }
   return Content("Error, not found!");
}
Ssheverdin
  • 111
  • 5
  • I'm not sure issue happened here in controller because it never comes in a controller I put a breakpoint to test it, it breaks on view on part where I'm trying to load my partial but we will see now.. – Roxy'Pro May 31 '17 at 22:49
  • as I expected it still fails on a place where I'm trying to load my partial view, it is not coming into controller's method UnReadEmails() .. – Roxy'Pro May 31 '17 at 22:55
  • Try to call it by itself "Massage/UnReadEmails" in url and see what happens, and make it work. If everything works in ActionResult @Html.Action("UnReadEmails", "Message") should work also. – Ssheverdin Jun 01 '17 at 17:08
  • Also you can try '@{Html.RenderAction("UnReadEmails", "Message")}', after making sure that ActionResult works. – Ssheverdin Jun 01 '17 at 17:14
0

Hi you may try by changing this line @Html.Action("UnreadEmails", "Message") to like this @{Html.Action("Category","Home");}

if the above doesnt work there are three other ways to render the partial view in mvc. those are

Html.RenderPartial  
Html.RenderAction
Html.Partial

you make use any one of the above and get your solution.

Good example with comparision can be found here:http://www.dotnettricks.com/learn/mvc/renderpartial-vs-renderaction-vs-partial-vs-action-in-mvc-razor

Hope it was helpful,kindly let me know your thoughts or feedbacks

Thanks Karthik

Karthik Elumalai
  • 1,574
  • 1
  • 11
  • 12
0

At the end I decided to skip my PartialViewResult method from a controller and do it on another way, because I've allready lost 2 days trying to find an answer and it wasn't there, but something else seems to be working, and it was this:

{@Html.RenderPartial("~/Views/Emails/_UnReadEmails.cshtml",EmailController.GetUnreadEmailsByUserId(User.Id))}

What I did there was next, I called directly GetUnreadEmaisByUserId method from my BussinesLogic part, and everything worked like charm.

And what we might do here to make this solution better is next:

In case we dont have a "main model" we could store that in a ViewBag, calling this from a MessageController/Email controller method

ViewBag.lstUnreadEmail‌s = E‌​mailController.Get‌‌​​Un‌​readEmailsByUserId(U‌​ser.Id);
And in the View use this

{@Html.RenderPartial("~/Views/Emails/_UnReadEmails.cshtml",ViewBag.lstUnreadEmail‌​s}
Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102