2

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
3
  • You are trying to pass your controller JSON data from the view and it is getting serialized as null? Commented Dec 7, 2011 at 22:21
  • I'm trying to pass my jquery get() a json result from my controller then pass the json object to another controller method via $load(). Commented Dec 7, 2011 at 23:07
  • Please include the HTTP request body sent to the server when step 3 executes. Commented Dec 7, 2011 at 23:44

3 Answers 3

1

Try this:

function loadRollupGridView () {
    $.ajax({
        url: '@Url.Action("GetCerts")',
        type: 'GET',
        success: function(certRollupViewModel) { 
            $('#rollupgridview')
                .load('@Url.Action("GetCertRollupView"), certRollupViewModel)');
        },               
        error: function () {
            $('#rollupgridview')
                .html('<div class="error">Something went wrong...</div>');
        }
    });
}

I think the main issue is that you are passing the variable data to the Controller, not certRollupViewModel like it is expecting.

This is what your JSON object should look like when being passed to the controller:

{"certRollupViewModel": 
  {"CertRollups":
    [
      {"TotalCpm": "25.35", "TotalIO": "380.23"}, 
      {"TotalCpm": "25.35", "TotalIO": "380.23"},
      {"TotalCpm": "25.35", "TotalIO": "380.23"}
    ]
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe I missed where you are serializing your data to send over? Check out how the form is serialized

Serializing form data doesn't work in asp.net mvc app

In your code above I don't see you specifying data to send over to the controller

2 Comments

Hi, thx for the response. Perhpas my vernacular is not on point but I'm creating a json object from mvc.net werld so I'm assuming that there is some type of json serialization involved to conver from .net to json.
but you still aren't sending a json object to eh url you are requesting from Url.Action hence your controller never ever ever receives serialized json to GetCertRollUpView. You need to serialize your form data before you send it to GetCertRollUpView. See the link I specified above.
0
JsonResult ActionRoutine()
{
   ...
   return Json( data );
}

UPDATE: I think you're looking at returning JSON to an action method. I've had success, making the arguments with matching args to JSON params as the method signature.

3 Comments

I think he's trying to say that he wants to pass his controller JSON data from the view but it is not working, not the other way around.
Oh... From what I remember you can grab it as a string or declare matching arguments that MVC matches up for you.
Hi! Thx for the replies. JsonResult return didn't matter as the return type.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.