【问题标题】:jQuery Ajax, MVC and the QuerystringjQuery Ajax、MVC 和查询字符串
【发布时间】:2011-11-16 04:22:27
【问题描述】:

我在 ASP.NET MVC3 网站的视图中有以下 ajax 函数。这个函数是从页面顶部的列表中调用的,效果很好。但是我需要查询字符串中的另一个值,它需要传递给控制器​​函数。我如何做到这一点?

jQuery 函数

   function ShowDeals(itemNo) {
    //get the vendor



    var dialogDIV = $('<div></div>');
    $.ajax(
        {
            url: '/Deal/Index',
            type: "POST",
            data: ({ toets: itemNo }),
            dataType:'html',
            success: function (result) {
                $(dialogDIV).html(result);
                $(dialogDIV).dialog({
                    position : [,100],
                    error: function (req, status, error) {
                        alert(error);
                    }
                });
            },
            error: function (req, status, error) {
                alert(error);
            }
        })
        return false;
    }   

控制器动作

public ActionResult Index(int toets,string vendorNo)
    {
        string temp = toets.ToString();
        string tempdd = Request.QueryString["vendorNo"];
        //return Content("1212121212");
        return PartialView();
    }

toets 参数是从 ajax 函数传递的,但我现在需要 Querystring 中的 vendorNo。

谢谢

编辑:我知道我可以添加一个 javascript 函数来从查询字符串中获取值,但这是最好的方法吗?

【问题讨论】:

    标签: c# jquery asp.net asp.net-mvc-3 razor


    【解决方案1】:

    您可以像传递 toets 参数一样传递它:

    var vendor = $("#TheElementThatHoldsYourVendor").text();
    $.ajax(
    {
        url: '/Deal/Index',
        type: "POST",
        data: ({ toets: itemNo, vendorNo: vendor }),
        ....
    });
    

    然后你将它作为第二个参数,你不必访问QueryString 来获取它。

    编辑
    要使用 javascript 获取 url 参数,您可以使用此方法 (from here)

    function getUrlVars()
    {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for(var i = 0; i < hashes.length; i++)
        {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    }
    

    然后这样称呼它:

    $.ajax({
        ...
        data: { toets: itemNo, vendorNo: getUrlVars()['vendorNo'] }
        ...
    });
    

    【讨论】:

    • 供应商未显示在页面上。发生的情况是用户登录到系统,显示供应商列表,然后使用 Actionlink 将用户重定向到 Items 页面,并从这些项目之一调用 Ajax 方法。所以 $("#TheElementThatHoldsYourVendor") 是查询字符串。
    • 哦,我明白了。但这不会在您的 AJAX 的查询字符串中,因此您需要使用 js 手动传递它。
    • @CaptainOrgange:进行了编辑以显示如何使用 javascript 获取 url 参数
    【解决方案2】:
    function ShowDeals(itemNo) {
        //get the vendor
    
        var venderNo = $("#TheElementThatHoldsYourVendor").text();
    
        var dialogDIV = $('<div></div>');
        $.ajax({
            url: '/Deal/Index?vendorNo=' + venderNo,
            type: "POST",
            data: ({ toets: itemNo }),
            ...
    }   
    

    如果控制器操作正在寻找查询字符串,则需要将查询字符串添加到 url。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-01
      相关资源
      最近更新 更多