2014-09-19 23 views
2

型號:阿賈克斯得到控制對象可以查看

 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在文本框中。

回答

2

嘗試從控制器

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