1

my ajax code

function gonder() {
    var params = {
        DonationInfo: {
            name: $('#name').val(),
            lastname: $('#lastname').val(),
            phone: $('#phone').val(),
            type: $('#type').val(),
            amounth: $('#amounth').val(),
            quentity: $('#quentity').val()
        }
    };
    $.ajax({
        url: '@Url.Action("Index", "Benafactor")',
        type: 'POST',
        async: true,
        data: JSON.stringify(params),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (data) {
            alert(data.success);
            $('#target').html(data);
        },
        error: function () {
            alert("error");
        }
    });
}

MY Controller

   [System.Web.Http.HttpPost]
    public ActionResult Index([FromBody] Mymodel data)
    {
        return Json(new { success = true });

    }

also i tried with string

and here is Mymodel

  public class Mymodel
{
    public string name { get; set; }
    public string lastname { get; set; }
    public string phone { get; set; }
    public string type { get; set; }
    public string amounth { get; set; }
    public string quentity { get; set; }
}

i tried hard , looked for the all same question but nothing work for me please help i can see the data in request payload but can't get the parameters into controller

2
  • use jquery post check this link api.jquery.com/jquery.post Commented May 3, 2016 at 9:21
  • Even easier than the answers, Remove contentType option and use data: $('form').serialize(), (.serialize()` correctly serializes your form controls to json) Commented May 3, 2016 at 9:45

4 Answers 4

3

When you serialize object(params) as json then the structure of the mvc ActionResult paramter model(Mymodel) and the object(params) need to be same structure, in your code params and Mymodel is not same structure. Make it same will solve the problem

var params = {
            name: $('#name').val(),
            lastname: $('#lastname').val(),
            phone: $('#phone').val(),
            type: $('#type').val(),
            amounth: $('#amounth').val(),
            quentity: $('#quentity').val()
       };
Sign up to request clarification or add additional context in comments.

4 Comments

i will but have to wait for 8 minutes
better solution is given below
you are wellcome, happy coding :)
I think, to add impact, it would be beneficial to give an explanation as to what was wrong, rather than just giving the answer.
1

use this :

var params = {
        name: $('#name').val(),
        lastname: $('#lastname').val(),
        phone: $('#phone').val(),
        type: $('#type').val(),
        amounth: $('#amounth').val(),
        quentity: $('#quentity').val()
    }

for a complex object, its better to use variables than including it in ajax method

Comments

0

JSON.stringify is not necessarydata: params.DonationInfo

Comments

0
function gonder() {
    var data= {

            name: $('#name').val(),
            lastname: $('#lastname').val(),
            phone: $('#phone').val(),
            type: $('#type').val(),
            amounth: $('#amounth').val(),
            quentity: $('#quentity').val()

    };
    $.ajax({
        url: '@Url.Action("Index", "Benafactor")',
        type: 'POST',
        async: true,
        data: JSON.stringify(params),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (data) {
            alert(data.success);
            $('#target').html(data);
        },
        error: function () {
            alert("error");
        }
    });
}

Your controller will look like this

[HttpPost]
    public ActionResult Index(Mymodel data)
    {
        return Json(new { success = true });

    }

Comments

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.