【问题标题】:How to redirect to new page(along with model) on JQuery ajax call in MVC3如何在 MVC3 中的 JQuery ajax 调用上重定向到新页面(连同模型)
【发布时间】: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();
        }

    }

请帮忙。提前致谢

【问题讨论】:

  • IsDataValidRegistrationModel 的属性吗?
  • @Tieson 是的,但正如我再次回答您的答案时,我还需要将模型传递给控制器​​
  • 在视图中混合 Javascript? 让我毛骨悚然。我建议您宁愿使用data- 属性在您的视图中传递特定的 URL 并在脚本文件中读取它们。

标签: asp.net-mvc asp.net-mvc-3 jquery


【解决方案1】:

这就是我们需要做的。我们不能传递模型,只能传递链接重定向中的参数。我们必须使用 link.replace,因为您可以将变量直接传递到 Url.Action

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(IsDataValid){
              var link = "@Url.Action("Actionname", "Controllername", new { username = "1", phone ="2" })";
                   link = link.replace("1", data.username);
                   link = link.replace("2", data.phone);
                   alert(link);
                   window.location.href= link ;
            }
            else{
                //Show an error message without hiding the div
            }
        },
        failure: function () {                   
        }
    });

【讨论】:

  • 不知道你有没有看到上面问题的cmets,但看起来条件应该是if(data.IsDataValid){ ... }。我根本懒得更新我的答案...
【解决方案2】:

解决您现在可以回答的部分问题:

if(IsDataValid){
  // redirect to new page(
}
else{
    //Show an error message without hiding the div
}

要重定向,请使用window.location.replace('url to new page');

对于“错误消息”部分,我会使用 jQuery UI 来显示一个对话框,或者有一个隐藏的 div 用于插入错误消息,可能像这样:

$('#errors').html(data.Errors).show();

这假设您有某种名为Errors 的属性,其中包含RegistrationModel 上的验证消息。

【讨论】:

  • 要重定向我可以使用它,但这里的问题是我需要传递模型对象。
  • @user1678741 - 你不能通过重定向传递模型对象,至少在没有将它序列化为你可能不想做的 url 查询字符串的情况下不能。
猜你喜欢
  • 2012-04-24
  • 2011-01-19
  • 2013-08-11
  • 1970-01-01
  • 2018-04-17
  • 1970-01-01
  • 1970-01-01
  • 2011-08-18
  • 2011-07-03
相关资源
最近更新 更多