2

model :

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

controller:

  [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)
                }
                })

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.

1 Answer 1

2

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);
    }

Then use the response in the ajax success:

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

Hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.