本文介绍了从阿贾克斯得到控制对象查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模型:

        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在文本

And the problem is that i want to get this item.Name, item.Adres and item.Surname in 3 textboxes but when i type response.Adres i get nothing. When i type just response i get: MyNewProject.Models.Student in textbox.

推荐答案

尝试从控制器返回JSON结果

Try returning a JSON result from the controller

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

然后使用ajax的成功响应:

Then use the response in the ajax success:

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

希望它帮助!

这篇关于从阿贾克斯得到控制对象查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:49