【发布时间】:2012-09-29 00:07:00
【问题描述】:
我忘记了密码页面,用户输入用户名并单击“验证”按钮以检查他所在的组。根据组,我们需要显示不同的部分视图(现在假设它是电话编号)在页面中。填写有效详细信息后,成功时我将重定向到他将更新密码的新页面,失败时,我需要显示错误消息。
现在我很难编写突出显示的代码。
这是在提交按钮点击时触发的 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 是的,但正如我再次回答您的答案时,我还需要将模型传递给控制器
-
在视图中混合 Javascript? 让我毛骨悚然。我建议您宁愿使用
data-属性在您的视图中传递特定的 URL 并在脚本文件中读取它们。
标签: asp.net-mvc asp.net-mvc-3 jquery