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

2

On my homepage, there is a shortcut button that opens a modal, and within that modal it has a partialView that points to an action within another controller . Until then calm down.

<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 partial view being displayed correctly for inclusion of the information. After the inclusion, the routine executes savechanges correctly in controller , then it gives 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);
    }

In debug it returns to index , and at the moment of redrawing the screen with modal, it accuses the error:

Does anyone have any ideas for fixing this?

    
asked by anonymous 03.08.2015 / 17:44

1 answer

1

The error says it all. You can not use an Action call View to redirect requests. The reason for this is to avoid cascading redirects.

The correct one in your case would be the View main do POST (through the same modal). The modal can be assembled through a partial in JavaScript without necessarily calling an Action :

@Html.Partial("_MinhaModal", Model)
    
03.08.2015 / 18:37