3

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.

1
  • The default $.ajax() type is 'GET', so you'd need to set that to 'POST' if you're expecting only posted data. Commented Oct 21, 2013 at 12:39

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

this is a good. but how to send property "No" ? property "No" is a KeyValuePair.
No. question is Updated.
0

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.

Comments

0

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.

6 Comments

i changed " type: 'POST'" but doesn't working. why? Empty class are posted
i'll need more information than it doesn't work why. Give us something to work with, errors etc
Error does not happen.But the data is sent empty
cool, see updated code and open up your console, F12 and tell me what it responded with. Also, which browser are you using?
error : the server responded with a status of 404 (Not Found).I'm using Chrome
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.