阿贾克斯得到控制对象可以查看

问题描述:

型号:阿贾克斯得到控制对象可以查看

 public int Id { get; set; } 
     public string Name { get; set; } 
     public string Adres { get; set; } 
     public string Surname { get; set; } 

控制器:

[HttpGet] 
     public Student GetStudentById(int id) 
    { 
     var output = RepositoryFactory.Create<IStudentRepository>().GetByID(id); 
     return output; 
    } 

JS:

.click(function() { 
      $.ajax({ 
       type: "GET", 
       url: '/Admin/GetStudentById/', 
       data: { id: object_id }, 
       success: function (response) { 
        $('#toolbox-text').val(response) 
       } 
       }) 

而问题是,我要得到这个item.Name ,item.Adres和item.Surname在3个文本框中,但是当我键入response.Adres时,我什么也没有得到。当我输入只是响应我得到:MyNewProject.Models.Student在文本框中。

尝试从控制器

public ActionResult GetStudentById(){ 
      var output = RepositoryFactory.Create<IStudentRepository>().GetByID(id); 
      var result = new { Name = output.Name, Adres = output.Adres}; 
      return Json(result, JsonRequestBehavior.AllowGet); 
    } 

返回JSON结果。然后使用ajax的成功响应:

  success: function (response) { 
       $('#toolbox-text').val(response.Name) 
      } 

希望它能帮助!

+0

是的工作,谢谢:) – Lubudubu1010 2014-09-19 20:49:48