0

I want to pass a complex object from a Controller Method back to my Ajax call. As far as I know from several researches JSON is the thing of choice at this point.

Here's my Controller Method:

public JsonResult GetUserByData(string fn, string ln, string dep, string cc, string tel, string mail) {
        IList<Models.Person> userCollection = Models.Person.GetPersonsByData(fn, ln, dep, tel, cc, mail).ToList();
        if (userCollection.Count > 1) {
            return Json(new { Complex= true, HTML = PartialView("SomeView.cshtml", userCollection) });

        }
        else {
            return Json(new { Complex = false, HTML = PartialView("SomeOtherView.cshtml", userCollection.First()) });
        }

Here's my Ajax call:

$.ajax({
            url: 'Home/GetUserByData',
            type: 'POST',
            dataType: 'html',
            data: {
                fn: firstname,
                ln: lastname,
                dep: department,
                cc: costcenter,
                tel: telephone,
                mail: mail
            },
            success: function (data) {
                if (data.Complex)
                    $('#Customer_Person').html(data.HTML);
                else
                    $('#Notification_Area').html(data.HTML);
            }
        });

Back in my Script it seems that the Properties "Complex" and "HTML" can not be accessed - What am I doing wrong? Is this the best approach passing complex objects or are there other ways doing this?

Th1sD0t
  • 1,089
  • 3
  • 11
  • 37
  • did you check what response is being actualy returned in your browsers `developer` tools? – Pawan May 19 '15 at 04:59
  • 1
    possible duplicate of [MVC Return Partial View as JSON](http://stackoverflow.com/questions/4730777/mvc-return-partial-view-as-json) –  May 19 '15 at 06:15

1 Answers1

3

Since you are specifying the return type as HTML (dataType: 'html'), jQuery is treating the response as a string (and not JSON), so data.Complex won't evaluate to anything. If you were to try to parse the data as JSON it would likely error out as HTML and JSON don't play very well together. I would suggest returning the JSON first and making a subsequent call for the HTML or just selecting the correct template on the server and only sending the HTML.

edit: @Savaratkar is correct, you could also encode/escape your HTML and pass it via JSON.

Rob M.
  • 35,491
  • 6
  • 51
  • 50
  • Oh, my fault - sure the datatype has to be changed from html to json. Unfortunately it's not that easy to split those data into different calls because 1. Depending on the boolean value the data has to be placed on different areas inside my site, 2. My Controller Method searches the AD for some entrys - I'd rather keep the load on the AD as low as possible (So calling a second Method which would pass the actual data would be possible but less efficient). – Th1sD0t May 19 '15 at 05:11
  • 1
    Use some simple encoding method for your html string and decode it back in your client. – Savaratkar May 19 '15 at 05:21
  • I searched for some Methods to render a PartialView as a string but never successfull - may you can help once again? Thanks in advance! – Th1sD0t May 19 '15 at 05:59