我忘記了密碼頁面,用戶輸入用戶名並單擊「驗證」按鈕以檢查他所在的組。我們需要在頁面中顯示不同的部分視圖(現在我們假設它是電話號碼)。填寫有效的詳細信息後,成功後,我將重定向到一個新頁面,在那裏他將更新他的密碼,失敗時,我需要顯示錯誤消息。如何重定向到MVC3中的jQuery ajax調用的新頁面(以及模型)
現在我很難寫出突出顯示的代碼。
這裏是jQuery的AJAX功能的代碼觸發提交按鈕點擊
person = {UserName: $("#UserName").val(), Phone: $("#Phone").val() }
$.ajax({
type: 'POST',
url: '@Url.Action("ForgotPassword", "Customer")',
data: person,
dataType: 'json',
success: function (data) {
//This is the place I need help
if(data.IsDataValid){
// redirect to new page passing the model object
}
else{
//Show an error message without hiding the div
}
},
failure: function() {
}
});
這裏是代碼控制器動作
[HttpPost]
[ValidateInput(true)]
public ActionResult ForgotPassword(RegisterModel model)
{
if (Request.IsAjaxRequest())
{
Here is the logic which validates the user
return Json(new { regmodel = RegistrationModel })
}
//If it is not ajax call I want to return the view
if (isUsergroup1 == true)
{
return View("View1", RegistrationModel);
}
else if (isUsergroup2 == true)
{
return View("View2", RegistrationModel);
}
else
{
return View();
}
}
請幫助。在此先感謝
IsDataValid是RegistrationModel的屬性嗎? –
@Tieson是的,但是當我提到你的答案時,我還需要將模型傳遞給控制器 – Bob
在視圖中混合Javascript? **讓我毛骨悚然**。我建議你使用'data-'屬性來傳遞視圖中的特定URL並在腳本文件中讀取它們。 –