How can I serializes the following as a json result? The object is coming back to my controller as null.
public class CertRollupViewModel
{
public IEnumerable<CertRollup> CertRollups { get; set; }
}
public class CertRollup
{
public decimal TotalCpm { get; set; }
public decimal TotalIO { get; set; }
}
// The json obj leaves the controller method "GetAlerts" ok but not sure how to validate if the object
// is intact before it get's passed into the GetCertRollupView.
// The GetCertRollupView is where the json object is null
// Some button click...
$.get('@Url.Action("GetCerts")', **// STEP 1**
function (data) {
$("#rollupgridview").load('@Url.Action("GetCertRollUpView")', **// STEP 3**
data);
...
public ActionResult GetCerts() **// STEP 2**
{
...
return Json(CertRollupViewModelObject, JsonRequestBehavior.AllowGet);
}
public ActionResult GetCertRollUpView(CertRollupViewModel certRollupViewModel) **// STEP 4**
{
// certRollupViewModel IS NULL!!!
return PartialView("_CertRollUp", certRollupViewModel);
}
NOTE: The structure does get passed in correctly however, the values for CertRollup are 0’s.
Immediate window per Visual Studio:
?certRollupViewModel.CertRollups
Count = 1
[0]: {Models.CertRollup} ?CertRollupViewModel.CertRollups.First() {Models.CertRollup}
TotalCpm: 0
Try this:
I think the main issue is that you are passing the variable
datato the Controller, notcertRollupViewModellike it is expecting.This is what your JSON object should look like when being passed to the controller: