Reputation: 25
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 ?
Upvotes: 2
Views: 9234
Reputation: 37771
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'] }
...
});
Upvotes: 5
Reputation: 992
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.
Upvotes: -1