dimanche 12 juin 2016

ASP.NET5 MVC6: Pass object from view to controller always null


EDIT: I am able to pass some plain data if I use type: 'GET' in the ajax call and [HttpGET] in the Controller.

However, complex datatypes like my Summary class is still not working and everything is either 0 or NULL.

I have the following class:

public class Summary
    {
        public int Total { get; set; }
        public double Average { get; set; }
        public int Issues { get; set; }
        public int Fixed { get; set; }
        public double FixedPercentage { get; set; }
        public double TotalPercentage { get; set; }
        public List<Issues> IssuesList { get; set; }        
    }

public class Issues
{
       public string Name {get; set;}
       public int Code {get; set;}
}

And the following Action on my Controller:

[HttpGet]
public IActionResult GetSummary(Summary summaryBySource)
{
     Json(new
     {
         summary = "hi"
     });
}

Lastly, this is the ajax call I am using to return the object to the Controller:

$.ajax({
        type: 'GET',
        dataType: 'json',
        url: '/Summary/GetSummary/',
        data: JSON.stringify(summaryBySource),
        error: function (xhr) {
            alert("Wrong");
        },
        success: function (result) {
            alert("Yeah");

        },
        async: true,
        processData: true
    });

Once the ajax call gets called, it jumps into the controller function but all its properties are NULL. Why is that happening?

BTW: the variable summaryBySource is a global variable that is in the View at the moment that the ajax call is called and it contains the values that matches with the Summary object.

Also, I tried to do the same thing but instead of passing an object, just a dumb string and it also returns null...

Thanks!

EDIT: The content of summaryBySource is

It will be an Object type and...

Total: 0
Average: 0
Issues: 2
Fixed: 1
FixedPercentage: 50
TotalPercentage: 100
IssuesList: Array[1]
    0: Object
        Name: "Crash"
        Code: 1001

Aucun commentaire:

Enregistrer un commentaire