asp net mvc5: Secondary actions are not allowed to execute redirect actions

0

I'm developing with mvc5 and EF

I have a controller with the operations to edit a record something like this

        public ActionResult Edit(int id, string weblogin, byte tienda)
    {
        if (id <= 0)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Clientes clientes = db.Clientes.First(x => x.id == id && x.weblogin == weblogin && x.tienda == tienda);

        if (clientes == null)
        {
            return HttpNotFound();
        }
        return View(clientes);
    }

    // Para protegerse de ataques de publicación excesiva, habilite las propiedades específicas a las que desea enlazarse. Para obtener 
    // más información vea http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "id,nombre,apellido1,apellido2,....")] Clientes clientes)
    {
        if (ModelState.IsValid)
        {
            db.Entry(clientes).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(clientes);
    }

If I invoke it from the / Edit / parameters driver, it loads the corresponding form to edit the data and leaves me save the data that invokes the second action result

Now I try to put this from a form that contains some tabs the code is this

        <div role="tabpanel" class="tab-pane" id="second">
        @{
            Html.RenderAction("Edit", "Clientes", new
            {
                weblogin = Model.clientes.weblogin,
                id = Model.clientes.id,
                tienda = Model.clientes.tienda,
            });
        }
    </div>

The error you give me is this

[InvalidOperationException: No se permiten acciones secundarias para ejecutar acciones de redireccionamiento.] System.Web.Mvc.RedirectToRouteResult.ExecuteResult(ControllerContext context) +164

Thank you,

I forgot to comment that this code I have it in a tab and I want it to be loaded when entering the

@model testweb.Classes.claseClientesVentas
 

<div>

    <!-- Nav tabs -->
    <ul class="nav nav-tabs" role="tablist">
        <li role="presentation" class="active"><a href="#first" aria-controls="first" role="tab" data-toggle="tab">Datos cliente</a></li>
        <li role="presentation"><a href="#second" aria-controls="second" role="tab" data-toggle="tab">Editar cliente</a></li>
        <li role="presentation"><a href="#third" aria-controls="third" role="tab" data-toggle="tab">Ventas</a></li>

    </ul>

    <!-- Tab panes -->
    <div class="tab-content">
        <div role="tabpanel" class="tab-pane active" id="first">
            
            @{
                Html.RenderAction("Details", "Clientes", new { weblogin = Model.clientes.weblogin,
                                                               id = Model.clientes.id,
                                                               tienda = Model.clientes.tienda,
                                                               tab = true
                                                               
                });
            }
         </div>

        <div role="tabpanel" class="tab-pane" id="second">
           @{
               Url.Action("Edit", "Clientes", new
               {
                   weblogin = Model.clientes.weblogin,
                   id = Model.clientes.id,
                   tienda = Model.clientes.tienda,
               });
          }
        </div>



    <div role="tabpanel" class="tab-pane" id="third">
        33 <br />
         
    </div>
  </div>

</div>
    
asked by ilernet 16.05.2017 в 20:56
source

2 answers

2

Replaces Html.RenderAction with Url.Action

@{
   Url.Action("Edit", "Clientes", new
   {
     weblogin = Model.clientes.weblogin,
     id = Model.clientes.id,
     tienda = Model.clientes.tienda,
   });
}
    
answered by 16.05.2017 в 20:59
0

Have you tried decorating your controller method with [ChildActionOnly] ? You can see more information at ChildActionOnlyAttribute Class

[ChildActionOnly]
public ActionResult Edit(int id, string weblogin, byte tienda)
{
    if (id <= 0)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Clientes clientes = db.Clientes.First(x => x.id == id && x.weblogin == weblogin && x.tienda == tienda);

    if (clientes == null)
    {
        return HttpNotFound();
    }
    return View(clientes);
}
    
answered by 17.05.2017 в 13:04