यहाँ आप क्या कर सकते हैं:
[HttpPost]
public ActionResult Login(LoginModel model)
{
if (ModelState.IsValid)
{
// everything went fine and we want to redirect in this case =>
// we pass the url we want to redirect to as a JSON object:
return Json(new { redirectTo = Url.Action("SomeController", "SomeAction") });
}
else
{
// there was an error => add an error message
ModelState.AddModelError("login is fail")
}
// return a partial view instead of a full vire
return PartialView("Login",model)
}
और फिर आप सभी की जरूरत सफलता समारोह है:
@using (Ajax.BeginForm("Login", new AjaxOptions { HttpMethod = "POST", OnSuccess = "loginAjaxSuccess" }))
{
}
जिसमें आप परीक्षण कर सकते हैं जिस स्थिति में आप कर रहे हैं:
function loginAjaxSuccess(result) {
if (result.redirectTo) {
// the controller action returned a JSON result => it was a successful login
// => we redirect the browser to this url
window.location.href = result.redirectTo;
} else {
// the action returned a partial view with the form containing the errors
// => we need to update the DOM:
$('#Login').html(result);
}
}
वैसे भी अगर आप त्रुटि के मामले में अविभाज्य क्लाइंट साइड सत्यापन का उपयोग कर रहे हैं जहां आप रीफ्रेश कर रहे हैं
} else {
// the action returned a partial view with the form containing the errors
// => we need to update the DOM
$('#Login').html(result);
// Now that the DOM is updated let's refresh the unobtrusive validation rules on the form:
$('form').removeData('validator');
$('form').removeData('unobtrusiveValidation');
$.validator.unobtrusive.parse('form');
}
स्रोत
2012-09-26 10:24:47
सफलता समारोह में आप क्या करते हैं? क्या आप रीडायरेक्ट करते हैं? जो मैं समझता हूं उससे आप सत्यापन संदेशों में विफल होने पर त्रुटि संदेशों को प्रदर्शित करना चाहते हैं => आप लॉगऑन फॉर्म वाले आंशिक दृश्य को वापस करना चाहते हैं और फिर अपने DOM से संबंधित भाग को रीफ्रेश करना चाहते हैं। लेकिन सफलता के मामले के बारे में क्या? आप इस मामले में ग्राहक को वापस क्या करना चाहते हैं? एक दृश्य? –
मुझे फिर से चलाने के लिए डारिन धन्यवाद, मैं सफलता में रीडायरेक्ट करना चाहता हूं लेकिन विफलता में मैं केवल त्रुटि संदेश के साथ ऑनफेलर को कॉल करना चाहता हूं। वर्तमान में एप्लिकेशन में यह सिक्योर विधि पर कॉल करता है, यहां तक कि मॉडलस्टेट मान्य नहीं है – Shivkumar