2014-05-21 8 views
7

Ho la seguente vista, che contiene un Ajax.BeginForm: -RedirectToAction con Ajax.BeginForm, risultati inaspettati

@using (Ajax.BeginForm("ChangeDevicesSwitch", "Switch", new AjaxOptions 

{ 
    InsertionMode = InsertionMode.InsertBefore, 
    UpdateTargetId = "result", 
    LoadingElementId = "progress2", 
    HttpMethod= "POST" 
    , 
    OnSuccess = "createsuccess", 
    OnFailure = "createfail" 




})) 
//code goes here 
<p><img src="~/Content/Ajax-loader-bar.gif" class="loadingimage" id="progress2" /></p> 
<div id ="result"></div> 

e il seguente metodo d'azione che sarà chiamato dal Ajax.Bginform: -

public ActionResult ChangeDevicesSwitch(SwitchJoin s) 

     {//code goes here 
      try 
      { 
       var count = repository.changeDeviceSwitch(s.Switch.SwitchID, (Int32)s.GeneralSwitchTo, User.Identity.Name.Substring(User.Identity.Name.IndexOf("\\") + 1)); 
       repository.Save(); 
       return RedirectToAction("Details", new { id = s.GeneralSwitchTo }); 

      } 
      catch (Exception e) 

      { 
       return Json(new { IsSuccess = "custome", description = "Error occurred. Please check...." }, JsonRequestBehavior.AllowGet); 
      } 



     } 

lo script che sarà eseguito quando il successo di ritorno Ajax.BeginForm è: -

function createsuccess(data) { 
    if (data.IsSuccess == "Unauthorized") { 

     jAlert(data.description, 'Unauthorized Access'); 
    } 
    else if (data.IsSuccess == "False") { 

     jAlert('Error Occurred. ' + data.description, 'Error'); 
    } 
    else if (data.IsSuccess == "custome") { 

     alert(data.description); 

    } 
    else { 
     jAlert('Record added Successfully ', 'Creation Confirmation'); 
    } 

} 

curren sto affrontando un problema è che quando viene raggiunta la RedirectToAction, l'intera vista verrà visualizzata all'interno della vista corrente !! quindi c'è un modo per forzare la mia applicazione a non aggiornare la destinazione se viene restituito un RedirecttoAction?

+1

Non è possibile effettuare un reindirizzamento tramite ajax. Invece è possibile inviare l'url di reindirizzamento al client e nella funzione 'createsuccess' impostare' window.location' nell'url fornito. – Zabavsky

+0

puoi consigliarti di più con un codice di esempio –

risposta

13

restituire l'URL si vuole fare un redirect a dal metodo di azione quando l'operazione è riuscita:

public ActionResult ChangeDevicesSwitch(SwitchJoin s) 
{ 
    try 
    { 
     ... 
     return Json(new { RedirectUrl = Url.Action("Details", new { id = s.GeneralSwitchTo }) }); 
    } 
    ... 
} 

E nel createsuccess:

function createsuccess(data) { 
    if (data.RedirectUrl) 
     window.location.href = data.RedirectUrl; 
} 
+1

qui è ** un liner ** 'return JavaScript (" window.location = '"+ Url.Action (" Home "," Dettagli ") +"' ")' controllare [questo] (http://stackoverflow.com/questions/1538523/how-to-get-an-asp-net-mvc-ajax-response-to-redirect-to-new-page-instead-of-inser), la speranza aiuta qualcuno. – stom

0

nel mio caso, seguendo il metodo è stato elaborato

function OnSuccess(data) { 
    var json; 
     if (data.responseText instanceof Object) 
      json = data.responseText; 
     else 
      json = $.parseJSON(data.responseText); 

     if (json.RedirectUrl) 
      window.location.href = json.RedirectUrl; 
}