I have the controller like the below:
public ActionResult Save(string input, string name) {
//Some code
return PartialView();
}
And I need an ajax call to this controller method and pass the two arguments input and value
And my ajax call is like the below:
$.ajax({
url: '/Home/Save',
type: 'POST',
async: false,
dataType: 'text',
processData: false,
data: "input=" + JSON.stringify(data) + "&name =" + $("#name").val(),
success: function (data) {
}
});
I am unable to pass the value to the name parameter.. the value in the name parameter is becoming null .. please help me .. Thanks in advance
This question is related to
jquery
ajax
asp.net-mvc
model-binding
function final_submit1() {
var city = $("#city").val();
var airport = $("#airport").val();
var vehicle = $("#vehicle").val();
if(city && airport){
$.ajax({
type:"POST",
cache:false,
data:{"city": city,"airport": airport},
url:'http://airportLimo/ajax-car-list',
success: function (html) {
console.log(html);
//$('#add').val('data sent');
//$('#msg').html(html);
$('#pprice').html("Price: $"+html);
}
});
}
}
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "ChnagePassword.aspx/AutocompleteSuggestions",
data: "{'searchstring':'" + request.term + "','st':'Arb'}",
dataType: "json",
success: function (data) {
response($.map(data.d, function (item) {
return { value: item }
}))
},
error: function (result) {
alert("Error");
}
});
I did that with helping from this question
jquery get querystring from URL
so let see how we will use this function
// Read a page's GET URL variables and return them as an associative array.
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 now just use it in Ajax call
"ajax": {
url: '/Departments/GetAllDepartments/',
type: 'GET',
dataType: 'json',
data: getUrlVars()// here is the tricky part
},
thats all, but if you want know how to use this function
or not send all the query string parameters
back to actual answer
In addition to posts by @xdumain, I prefer creating data object before ajax call so you can debug it.
var dataObject = JSON.stringify({
'input': $('#myInput').val(),
'name': $('#myName').val(),
});
Now use it in ajax call
$.ajax({
url: "/Home/SaveChart",
type: 'POST',
async: false,
dataType: 'json',
contentType: 'application/json',
data: dataObject,
success: function (data) { },
error: function (xhr) { } )};
Source: Stackoverflow.com