skip to Main Content

My problem: I’m sending JSON data, but my JSON data is going “null”? I don’t understand why it’s going null.

I read to all documentation but I am new for jquery.
What am I missing?
I need to send JSON data for my controller.

Index.cshtml:

var UserData = {
    "CustomerID": "99999",
    "CustomerCode": "0",
    "UserID":"127",
    "StartDate": "02/09/2019",
    "FinishDate": "03/08/2020",
    "SuccesID": "1",
    "Cash": "1",
    "ProblemID": "1",
    "PageNo":"1",
    "DataNo":"1"
};
var userDataJson = JSON.stringify(UserData);
$.ajax({
    type: "POST",
    url: "/Problems/Search",
    contentType: "application/json",
    data: userDataJson,
    success: function (msg) {
        console.log(msg);
    },
    error: function (err) {
        alert("search error");
    }
});

Model:

public class ProblemsInput
{
    public string CustomerID { get; set; }
    public string CustomerCode { get; set; }
    public string UserID { get; set; }
    public string StartDate { get; set; }
    public string FinishDate { get; set; }
    public string SuccesID { get; set; }
    public string Cash { get; set; }
    public string ProblemID { get; set; }
    public string PageNo { get; set; }
    public string DataNo { get; set; }
}

Controller:

[HttpPost]
public IActionResult Search(ProblemsInput problemsinput)
{
    return View();
}

3

Answers


  1. Just apply FromBody attribute to the problemsinput parameter:

    [HttpPost]
    public IActionResult Search([FromBody]ProblemsInput problemsinput) 
    { 
        return View(); 
    }
    
    Login or Signup to reply.
  2. Dude. This problem is your UserData. Try this code:

    var problemsinput= {
        CustomerID: '99999',
        CustomerCode: '0',
        UserID:'127',
        StartDate: '02/09/2019',
        FinishDate: '03/08/2020',
        SuccesID: '1',
        Cash: '1',
        ProblemID: '1',
        PageNo: '1',
        DataNo: '1',
    };
    
     $.ajax({
         type: "POST",
         url: "/Problems/Search",
         contentType: "application/json",
         data: {problemsinput},
         success: function (msg) {
             console.log(msg);
         },
         error: function (err) {
             alert("search error");
         }
    });
    

    If this doesnt work. Use [FromBody] attribute in Controller an try again. I hope this help you.

    Login or Signup to reply.
  3. You just need to add

     data: JSON.stringify({"problemsinput":userDataJson}),
    

    instead of data: userDataJson,
    This will solve the problem.

    Ajax call will be

    var userDataJson = JSON.stringify(UserData);
         $.ajax({
             type: "POST",
             url: "/Problems/Search",
             contentType: "application/json",
             data: JSON.stringify({"problemsinput":userDataJson}),
             success: function (msg) {
                 console.log(msg);
            },
            error: function (err) {
                alert("search error");
            }
        });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search