Child actions are not allowed to perform redirect actions. MVC 5

Asked

Viewed 651 times

2

On my homepage, there’s a shortcut button that opens a modal, and inside that modal is a partialView which points to a action within another controller. Until then quiet.

<div class="modal fade" id="modalCreate" tabindex="-1" role="dialog" aria-labelledby="createClasseModal">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-body">
                   @Html.Action("Create","Classes")
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                </div>
            </div>
        </div>
    </div> 

When opening the modal with the partial view being correctly displayed for inclusion of the information. After inclusion, the routine executes the savechanges correctly in the controller, then he gives a RedirectToAction to return to the home screen.

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "id,descricao,dia_1,dia_2,horario,dt_inicio,dt_termino,sala,limite_max,limite_min,preco,status,professor")] Classe classe)
    {
        ViewBag.professor = new SelectList(db.Pessoa, "id", "nome", classe.professor);
        if (ModelState.IsValid)
        {
            db.Classe.Add(classe);
            db.SaveChanges();
            return RedirectToAction("Index");

        }

        return View(classe);
    }

There in the debug he goes back to the index, and at the time of redesigning the screen with the modal, it accuses the error:

inserir a descrição da imagem aqui

Does anyone have any idea how to fix this?

  • Fix, in the controller is return RedirectToAction("Index");

1 answer

1


The error already says it all. You cannot use a Action call for a View to redirect requests. The reason is to avoid cascading redirects.

The correct in your case would be View main do the POST(by the modal itself). The modal can be assembled through a partial in Javascript without necessarily calling a Action:

@Html.Partial("_MinhaModal", Model)
  • Well, if I understand, because I’m a novice in c#, you’re suggesting that I swap Action for Partial in the partial call inside the modal window. That’s it?

  • No. I am suggesting that Modal be called by Javascript, and the Partial be called and written within a <div> occult.

  • how do I hide this <div> ?

  • <div style="display:none">.

  • It will be complicated because I don’t know javascript.

  • Here’s a good introduction to jQuery. Solves perfectly well for your case. ASP.NET MVC projects already come with jQuery installed by default.

  • 1

    I’ll do some research. Thanks for your help

  • More complicated than it helped, I don’t know where to start there. Does anyone have any more help?

  • @Pabloramonborges See this one here: http://jquerybrasil.org/tutorials/. Anything I can put in a chat with a class that can help you.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.