2

I have simple JavaScript code in which I want to get an object from a controller and parse it in the script, but in the script, I don't have access to any property of the object.

My object (in the controller):

public class userData
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string state { get; set; }
    public bool success { get; set; }
}

Script:

function  load()
{
    var sender = {
         userNo : $("#userNo").val(),
    }
    $.ajax({
        type : "POST",
        url : "testGetUser",
        data : sender,
        success:function(data)
            {
                if(1)
                {
                    $("#section #result0").html(data.firstName);
                    $("#section #result1").html(data.lastName);
                    $("#section #result2").html(data.state);
                    $("#section").slideDown().delay(1000).slideUp();
                }
            }
    });
}

Controller:

[HttpPost]
public userData testGetUser(long userNo)
{
    userData result = new userData();

    result.firstName = Session["firstName"].ToString();
    result.lastName = Session["lastName"].ToString();
    result.state = Session["country"].ToString();
    result.success = true;

    return result;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
itmanir
  • 167
  • 1
  • 3
  • 14
  • First, since you are only trying to get the user's data you should use a GET request instead of a POST request. – Shai Aharoni Jul 15 '13 at 11:33
  • 1
    Secondly, serialize model to JSON: http://stackoverflow.com/questions/6201529/turn-c-sharp-object-into-a-json-string-in-net-4 –  Jul 15 '13 at 11:33

1 Answers1

5

In the controller, use this:

[HttpPost]
public JsonResult testGetUser(long userNo)
{
    userData result = new userData();

    result.firstName = Session["firstName"].ToString();
    result.lastName = Session["lastName"].ToString();
    result.state = Session["country"].ToString();
    result.success = true;

    return Json(result);
}

It seems you are not using the variable userNo, so you better use a GET request like this:

public JsonResult testGetUser(/*remove parameter*/)
{
    userData result = new userData();

    result.firstName = Session["firstName"].ToString();
    result.lastName = Session["lastName"].ToString();
    result.state = Session["country"].ToString();
    result.success = true;

    return Json(result, JsonRequestBehavior.AllowGet);
}

The JavaScript now wants to be this:

function  load()
{
    $.ajax({
        type : "GET",
        url : "testGetUser",
        success:function(data) {
            $("#section #result0").html(data.firstName);
            $("#section #result1").html(data.lastName);
            $("#section #result2").html(data.state);
            $("#section").slideDown().delay(1000).slideUp();
        }
    });
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jonny Piazzi
  • 3,684
  • 4
  • 34
  • 81
  • Another one side-note: since element id on page should be unique, it's a bad practise to pass more then one id to selector, direct `$("#result0")` will be faster in selector parsing and faster in node search. – Tommi Jul 15 '13 at 11:57