Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery send class as POST parameters

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.

like image 243
john Avatar asked Mar 24 '26 00:03

john


1 Answers

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

like image 156
levelnis Avatar answered Mar 26 '26 17:03

levelnis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!