2014-05-21 25 views
7

Ben bir Ajax.BeginForm içeren aşağıdaki görünümü,: -RedirectToAction, beklenmeyen sonuçlar

@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> 

ve Ajax.Bginform çağrılabilir olacak aşağıdaki Eylem Yöntemi: -

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); 
      } 



     } 

Ajax.BeginForm dönüş başarı olduğunda çalışacak komut dosyası: -

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 Bir sorunla karşı karşıyayım tedir ki RedirectToAction eriştiğinde, tüm görünüm geçerli görünümde görüntülenecektir! Bir RedirecttoAction döndürülürse başvurumu hedeflememeye zorlamam için bir yol var mı?

+1

ajax üzerinden yönlendirme yapamazsınız. Bunun yerine yönlendirme URL'sini istemciye geri gönderebilir ve 'createuccess' işlevinde' window.location 'öğesini sağlanan URL'ye ayarlayabilirsiniz. – Zabavsky

+0

Örnek kod –

cevap

13

İade Eğer bir yönlendirme yapmak istiyorum url eylem yönteminden işlemi başarılı zaman:

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

Ve createsuccess yılında: benim durumumda

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

ile bu konuda daha fazla tavsiye verebilir ** bir liner ** 'dönüş JavaScript (" window.location = '"+ Url.Action (" Ev "," Ayrıntılar ") +"' ")' kontrol edin [this] (http://stackoverflow.com/questions/1538523/how-to-get-an-asp-net-mvc-ajax-response-to-redirect-to-new-page-instead-of-inser), birisine yardım eder. – stom

0

aşağıdaki yöntem çalıştı edildi

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; 
}