Reputation: 801
I want to send class as post ajax parameters.
my class :
public class PageInput
{
public string ActionName { get; set; }
public string Controller { get; set; }
public int CountPage { get; set; }
public int CountRecordInPage { get; set; }
public KeyValuePair<string, short> No { get; set; }
}
ajax code :
$.ajax({
url: '@Url.Action("GetPageNumbers", "MyClasses")',
data: '@Model.PageInput',
success: function (data) {
alert(data);
}
});
action :
public ActionResult GetPageNumbers(PageInput pageInput)
{
return PartialView("_PagingNumbers", pageInput);
}
doesn't working. But why?
When data are received by the actionResult are empty!!But the data are correct before sending.
Upvotes: 3
Views: 4283
Reputation: 7705
You need to create an object within your JavaScript code and send that across instead:
$.ajax({
url: '@Url.Action("GetPageNumbers", "MyClasses")',
data: {
ActionName: '@Model.ActionName',
Controller: '@Model.Controller',
CountPage: @Model.CountPage,
CountRecordInPage: @Model.CountRecordInPage,
},
success: function (data) {
alert(data);
}
});
You also need to add the [HttpPost]
attribute to your controller action
Upvotes: 2
Reputation: 28076
My guess would be that you aren't posting to your url
$.ajax({
url: '@Url.Action("GetPageNumbers", "MyClasses")',
data: '@Model.PageInput',
type: 'POST',
success: function (data) {
console.info(data);
}
});
Now it will post.
Upvotes: 0
Reputation: 666
you should use following pattern:
$.ajax({
url: '@Url.Action("GetPageNumbers", "MyClasses")',
data: {"PageInput.CountPage": "@Model.PageInput.CountPage", "PageInput.CountRecordInPage":"@Model.PageInput.CountRecordInPage" },
success: function (data) {
alert(data);
}
});
I think It will work for you. While Ajax post it is necessary that you should use name of the class in AJAX post parameters.
Upvotes: 0