0

I ma making a simple post request to my controller which is returning a json object that I am trying to parse, that is not working.

public ActionResult GetViewEditData(int machineID)
        {
            ViewBag.MachineData = Json(PMIComputerModel.GetViewEditDetails(machineID));
            return View("GetViewEditData");
        }

Ajax call
function GetDetails(crntAnc, mode) {
        var id = $(crntAnc).parents('tr:first').find('td:first').attr('id');
        alert(id);
        $.ajax({
            url: '/Home/GetViewEditData/',
            contentType: "application/json; charset=utf-8",
            type: 'POST',
            data: "{'machineID': '"+ id +"'}",
            success: function () {

            },
            error: function (e, jqxhr, settings, exception) {
                if (jqxhr != null)
                    alert(jqxhr.responseText);
            }
        });
var data = '@ViewBag.MachineData';
        alert(data);
        alert(jQuery.parseJSON(data));
        }

In the error block , it is coming as undefined. All i want do is parse my josn data in my javascript.

The class that is being serialized :

 public class PMICheckListWrapper
        {
            public PMIMachine Machine { get; set; }
            public List<PMICheckListResponse> CheckResponseList { get; set; }
        }

PS: Edit: Since I was not accessing in success function and also not returning any thing I have taken the pasring logic outside the ajax post but still it is coming

var data = '@ViewBag.MachineData';
        alert(data); // blank
        alert(jQuery.parseJSON(data)); //null
ankur
  • 4,565
  • 14
  • 64
  • 100
  • 1
    there is no need to to parse it again, because `dataType: 'json',` is already is there – Pranav C Balan Jan 21 '14 at 05:54
  • i am accessing data via viewbag it is not going in that code block\ – ankur Jan 21 '14 at 05:54
  • ok pranav, how can i access it i am passing the json object to viewBag and returning the view then trying to accesss it. – ankur Jan 21 '14 at 05:55
  • @ankur can u post your data value – iJade Jan 21 '14 at 05:58
  • You're not passing anything back from your AJAX call, your success function accepts no parameters, you could move `var data = '@ViewBag.MachineData'` outside of your AJAX call since it is rendered with the rest of the page, not on AJAX call – jammykam Jan 21 '14 at 06:00
  • ok then will i be able to access the var data = '@ViewBag.MachineData' and parse it – ankur Jan 21 '14 at 06:02
  • @iJay i am accessing data via viewbag like this '@ViewBag.MachineData' it is not going in that code block – ankur Jan 21 '14 at 06:02
  • @ankur you are doing an `alert(data);` post dat value – iJade Jan 21 '14 at 06:03
  • yes but i am doing that after this var data = '@ViewBag.MachineData'; alert(data); if i get the data value then i am trying to alert and see if it has got some value also i am alert(jQuery.parseJSON(data)); this is what i want to achieve in this ... – ankur Jan 21 '14 at 06:05
  • remove `dataType: 'json',` then alert data and post here what you are getting in the popup – Pranav C Balan Jan 21 '14 at 06:06
  • have a look at this . Its might help http://stackoverflow.com/questions/5980389/proper-way-to-use-ajax-post-in-jquery-to-pass-model-from-strongly-typed-mvc3-vie – iJade Jan 21 '14 at 06:09
  • @PranavRam it is coming alert(data); as blank and this is coming as alert(jQuery.parseJSON(data)); null.... – ankur Jan 21 '14 at 06:15
  • 1
    NEVER use `alert` for debugging, but use `console.log`. You can't see objects in alert whereas you can in the console. – AlexB Jan 21 '14 at 10:20
  • What is the return Status code from your AJAX code? It will never go into success unless 200 is returned – jammykam Jan 21 '14 at 15:16

1 Answers1

-1

I think you have to return a partial view

public ActionResult GetViewEditData(int machineID)
        {
            // ViewBag.MachineData = Json(PMIComputerModel.GetViewEditDetails(machineID));
            return PartialView("GetViewEditData");
        }

and in success block,

success: function (data) {

                $('#dvViewBox').html(data);
            },

or if you want to return Json itself,

var result = PMIComputerModel.GetViewEditDetails(machineID);
return Json( new { value = result },JsonRequestBehavior.AllowGet);

and in success block,

success: function (data) {
 alert(jQuery.parseJSON(data.value));
  // do your logic     
  },
ssilas777
  • 9,672
  • 4
  • 45
  • 68
  • no i don't need partial view , since it is not needed as per design. I am just trying to access the data ,If i am able to access my json data then i will parse and do the businees logic on it – ankur Jan 21 '14 at 06:01
  • Hard to understand from your question, in that case you return Json, not an entire view that's why it showing error. – ssilas777 Jan 21 '14 at 06:02
  • It should go something like I edited, not tested though.hope it helps. – ssilas777 Jan 21 '14 at 06:12
  • can you provide the complete method with post and response ,also how to access it so that it will resolve the issue... – ankur Jan 21 '14 at 06:17