2014-05-21 8 views
7

मेरे पास निम्न दृश्य है, जो एक Ajax.BeginForm शामिल हैं: -RedirectToAction, अप्रत्याशित परिणाम

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

और निम्नलिखित कार्रवाई विधि जो 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); 
      } 



     } 

स्क्रिप्ट जो जब 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 मैं एक समस्या का सामना कर रहा हूं यह है कि जब RedirectToAction तक पहुंच जाता है, तो संपूर्ण दृश्य वर्तमान दृश्य के अंदर प्रदर्शित किया जाएगा !! तो अगर कोई रीडायरेक्टोएक्शन वापस लौटाया गया है तो लक्ष्य को अपडेट न करने के लिए मेरे एप्लिकेशन को मजबूर करने का कोई तरीका है?

+1

आप AJAX के माध्यम से रीडायरेक्ट नहीं कर सकते हैं। इसके बजाय आप क्लाइंट को रीडायरेक्ट यूआरएल वापस भेज सकते हैं और 'createsuccess' फ़ंक्शन' प्रदान किए गए यूआरएल में 'window.location' सेट कर सकते हैं। – Zabavsky

+0

क्या आप इस पर अधिक नमूना कोड –

उत्तर

13

वापसी यूआरएल आप एक रीडायरेक्ट बनाना चाहते कार्रवाई विधि से जब आपरेशन सफल रहा करने के लिए:

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

और में createsuccess:

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

के साथ सलाह दे सकते हैं ** एक लाइनर ** 'वापसी जावास्क्रिप्ट (" window.location = '"+ url.Action (" Home "," विवरण ") +"' ")' ' [यह] जांचें (http://stackoverflow.com/questions/1538523/how-to-get-an-asp-net-mvc-ajax-response-to-redirect-to-new-page-instead-of-inser), उम्मीद किसी की मदद करता है। – stom

0
मेरे मामले में

, निम्न विधि काम किया गया था

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; 
} 
संबंधित मुद्दे