2

I have the following ajax function on my view in a ASP.NET MVC3 website. This function is called from a list in the top of the page, and it works great. But I need another value from the querystring, that needs to be passed to the controller function. How Do i achieve this ?

jQuery function

   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;
    }   

Controller Action

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

The toets parameter is passed from the ajax function, but I now need the vendorNo that is in the Querystring.

Thanks

EDIT : I know I can add a javascript function to get the value from the querystring, but is that the best way to do this ?

2 Answers 2

5

You can pass that in the same way you pass in your toets param:

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

And then you'd have it as the second parameter, you don't have to access the QueryString to fetch it.

EDIT
To fetch a url parameter using javascript, you could use this method (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;
}

And then call it like this:

$.ajax({
    ...
    data: { toets: itemNo, vendorNo: getUrlVars()['vendorNo'] }
    ...
});
Sign up to request clarification or add additional context in comments.

3 Comments

The vendor is not displayed on the page. What happen is the user log into the system, a list of vendors is displayed, using an Actionlink the user is then redirected to the Items page, and the Ajax method is called from one of these items. So the $("#TheElementThatHoldsYourVendor") is the querystring.
Oh, I see. But that won't be in the querystring of your AJAX, so you need to pass that in manually with js.
@CaptainOrgange: Made an edit to show how to fetch url params with javascript
-1
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 }),
        ...
}   

If the controller action is looking for a query string, you need to add the query string to the url.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.