Here is the code for how you send data from Controller to json:
$.ajax({
url: '@Url.Action("GetData", "Home")',
type: "GET",
success: function (result) {
$("#somediv").append(result.FirstName);
$("#somediv").append(result.LastName);
$("#somediv").append(result.Age);
}
});
Consider a class like the one below....
public class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
your action should look like this.
public JsonResult GetData()
{
User user = new User();
user.FirstName = "Yasser";
user.LastName = "Shaikh";
user.Age = 100;
return Json(user, JsonRequestBehavior.AllowGet);
}
Further Reading
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…