MVC RedirectToAction通过knockoutjs阿贾克斯jQuery的电话不能正常工作 [英] MVC RedirectToAction through ajax jQuery call in knockoutjs is not working

查看:118
本文介绍了MVC RedirectToAction通过knockoutjs阿贾克斯jQuery的电话不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个MVC3 Web应用程序,我使用knockoutjs。有在应用两种观点。 SetUpNewCompany和ManageAccount。成立一个新的公司用户首先进入帐号并点击搜索。如果该帐户数已经存在,用户可以在一个按钮点击进入ManageAccount视图。在SetUpNewCompanyController我重定向使用RedirectToAction方法。然而,在执行中ManageAccount的索引2时的动作的观点并没有显示出来。如果我输入完整的URL的视图。

SetUpNewCompanyController.cs

  [HttpPost]
 公共RedirectToRouteResult RedirectToManageAccount(字符串使用accountNumber)
 {
        返回RedirectToAction(索引2,新RouteValueDictionary(新{控制器=
            ManageAccount,companyId =7e96b930-a786-44dd-8576-052ce608e38f}));
 }
 

这上面调用一个按钮被点击时,下方的功能

  self.redirectToManageAccount =功能(){
        VAR了accountNumber =7e96b930-a786-44dd-8576-052ce608e38f;
        $阿贾克斯({
            键入:POST,
            网址:/ SetUpNewCompany / RedirectToManageAccount
            数据:{了accountNumber:使用accountNumber},
            成功:功能(数据){
            },
            错误:函数(){
            }
        });
 }
 

ManageAccountController.cs

 公众的ActionResult索引2(字符串companyId)
    {
        VAR视图模型=新Models.Index();

        名单<字符串> compList =新的名单,其中,字符串>();
        compList.Add(MyCompany的);

        名单<字符串> usersList =新的名单,其中,字符串>();
        usersList.Add(用户1);

        viewModel.Users = usersList;
        viewModel.Companies = compList;
        viewModel.CompanyId = companyId;
        viewModel.Role =基于role1;

        返回视图(ManageAccount,视图模型);
    }
 

生成的URL是

 的http://本地主机:53897 / ManageAccount /索引2 companyId = 7e96b930-a786-44dd-8576-
052ce608e38f
 

在Firebug显示控制台窗口

  GET的http://本地主机:53897 / ManageAccount /索引2 companyId = 7e96b930-a786-44dd-8576-
052ce608e38f 200 OK和微调保持spinng
 

另外,我怎么网址,而不是下面一个与查询字符串

 的http://本地主机:53897 / ManageAccount /索引2 / 7e96b930-a786-44dd-8576-052ce608e38f
 

解决方案

由于您使用AJAX调用 RedirectToManageAccount 操作方法,你是对自己负责处理其响应和为你的成功处理函数是空的,你有效地忽略任何到达作为响应。

如果您想从Ajax响应处理程序中强制重定向,我建议

  1. 修改你的操作方法如下:

      [HttpPost]
    公众的ActionResult RedirectToManageAccount(字符串使用accountNumber)
    {
        VAR的redirectUrl =新UrlHelper(Request.RequestContext).Action(索引2,ManageAccount,新{companyId =7e96b930-a786-44dd-8576-052ce608e38f});
        返回JSON(新{URL =的redirectUrl});
    }
     

  2. 更​​新您的AJAX调用这样

      self.redirectToManageAccount =功能(){
      VAR了accountNumber =7e96b930-a786-44dd-8576-052ce608e38f;
      $阿贾克斯({类型:POST,
               网址:/ SetUpNewCompany / RedirectToManageAccount
               数据:{了accountNumber:使用accountNumber},
               数据类型:JSON,
               成功:函数(响应){
                   window.location.href = response.Url;
               },
               错误:函数(){
               }
      });
    }
     

关于你的第二个问题:

  

另外,我怎么下面的网址,而不是一个与查询字符串   的http://本地主机:53897 / ManageAccount /索引2 / 7e96b930-a786-44dd-8576-052ce608e38f

您只需要定义此URL适当的路由条目在你的的RegisterRoutes()功能:

  routes.MapRoute(空,
                ManageAccount /索引2 / {companyId},
                新{控制器=ManageAccount
                      行动=索引2}
);
 


修改:随着AJAX调用只能用来打电话的操作方法,这会导致一个重定向,可以将其简化,在接下来的方式,只要你在这一点上(在客户端)知道 companyId

  self.redirectToManageAccount =功能(){
    变种companyId =12345;
    window.location.href ='@(Html.ActionUri(索引2,ManageAccount))companyId =?'+ companyId;
}
 

在这里我用这个扩展方法

 公共静态字符串ActionUri(这HtmlHelper的HTML,串动,串器)
{
    返回新UrlHelper(html.ViewContext.RequestContext).Action(动作,控制器);
}
 

I am building a MVC3 web application and I am using knockoutjs. There are two views in the application. SetUpNewCompany and ManageAccount. To Set up a new company the user first enters the account number and clicks search. If the account number already exists the user can click on a button to go to the ManageAccount view. In the SetUpNewCompanyController I redirect using the RedirectToAction method. However, when the Index2 action in ManageAccount is executed the view is not displayed. If I type in the complete URL the view is displayed.

SetUpNewCompanyController.cs

[HttpPost]
 public RedirectToRouteResult RedirectToManageAccount(string accountNumber)
 {
        return RedirectToAction("Index2", new RouteValueDictionary(new {controller=
            "ManageAccount", companyId = "7e96b930-a786-44dd-8576-052ce608e38f" }));
 }

This above is called by the function below when a button is clicked

self.redirectToManageAccount = function () {
        var accountNumber = "7e96b930-a786-44dd-8576-052ce608e38f";
        $.ajax({
            type: "POST",
            url: "/SetUpNewCompany/RedirectToManageAccount",
            data: { accountNumber: accountNumber },
            success: function (data) {
            },
            error: function () {
            }
        });
 }

ManageAccountController.cs

public ActionResult Index2(String companyId)
    {
        var viewModel = new Models.Index();

        List<String> compList = new List<String>();
        compList.Add("MyCompany");

        List<String> usersList = new List<String>();
        usersList.Add("User1");

        viewModel.Users = usersList;
        viewModel.Companies = compList;
        viewModel.CompanyId = companyId;
        viewModel.Role = "Role1";

        return View("ManageAccount",viewModel);
    }

The URL that is generated is

http://localhost:53897/ManageAccount/Index2?companyId=7e96b930-a786-44dd-8576-
052ce608e38f

The console window in Firebug shows

GET http://localhost:53897/ManageAccount/Index2?companyId=7e96b930-a786-44dd-8576-
052ce608e38f 200 OK and the spinner keeps spinng

Also, how do I get the URL below instead of the one with querystring

http://localhost:53897/ManageAccount/Index2/7e96b930-a786-44dd-8576-052ce608e38f

解决方案

Since you use AJAX to call the RedirectToManageAccount action method, you are responsible for handling its response yourself and as your success handler function is empty, you are effectively ignoring whatever arrives as a response.

If you want to force a redirect from within the AJAX response handler, I suggest

  1. Modifying your action method as follows

    [HttpPost]
    public ActionResult RedirectToManageAccount(string accountNumber)
    {
        var redirectUrl = new UrlHelper(Request.RequestContext).Action("Index2", "ManageAccount", new { companyId = "7e96b930-a786-44dd-8576-052ce608e38f" });
        return Json(new { Url = redirectUrl });
    }
    

  2. Updating your AJAX call in this way

    self.redirectToManageAccount = function () {
      var accountNumber = "7e96b930-a786-44dd-8576-052ce608e38f";
      $.ajax({ type: "POST",
               url: "/SetUpNewCompany/RedirectToManageAccount",
               data: { accountNumber: accountNumber },
               dataType: 'json',
               success: function (response) {
                   window.location.href = response.Url;
               },
               error: function () {
               }
      });
    }
    

As for your second question:

Also, how do I get the URL below instead of the one with querystring http://localhost:53897/ManageAccount/Index2/7e96b930-a786-44dd-8576-052ce608e38f

You just have to define an appropriate route entry for this URL in your RegisterRoutes() function:

routes.MapRoute(null,
                "ManageAccount/Index2/{companyId}",
                new { controller = "ManageAccount",
                      action = "Index2" }                            
);


EDIT: As your AJAX call only serves to call an action method which causes a redirect, you can simplify it in a following way, provided you in this point (on the client side) know the companyId already:

self.redirectToManageAccount = function () {
    var companyId = "12345";
    window.location.href = '@(Html.ActionUri("Index2", "ManageAccount"))?companyId=' + companyId;
}

where I used this extension method

public static string ActionUri(this HtmlHelper html, string action, string controller)
{
    return new UrlHelper(html.ViewContext.RequestContext).Action(action, controller);
}

这篇关于MVC RedirectToAction通过knockoutjs阿贾克斯jQuery的电话不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆