The idea is simple.
I want to wrap a Json data and using jQuery Ajax to pass the data to controller.
Here is the model definition.
public class SearchModel
{
public int answerValue;
public int year;
}
jQuery code:
$(function() {
var answerValue = $('#answerValue').val();
var year = $('#year').val();
var data = { "answerValue": answerValue, "year": year };
//data = answerValue;
$('#btn1').click(function () {
$.post("home/AnswersSearch", data, function (result) {
alert(result);
});
});
});
Code on the controller side:
[HttpPost]
public ActionResult AnswersSearch(SearchModel model)
{
//do something
return Content("1");
}
AnswersSearch()but all you have shown a method namedRespondentAnswersSearch()'@Url.Action("AnswersSearch", "Home")'to generate the correct url)SearchModelmodel modelis also a typo?public int answerValue { get; set; }