loki
loki

Reputation: 2966

How to get value from json data by using JsAction?

How to get Json value by using JsAction? How to acees firstname? i would like to show any value from Json value.

Controller:



   [JsAction()]
        [HttpGet]
        public JsonResult GetStudent(int a)
        {

            Student st =   new Student() { firstName = "yusuf", lastName = "karatoprak", SumHere = 5 };
            return Json(st, JsonRequestBehavior.AllowGet);
        }

View:



    $(document).ready(function () {
        JsActions.Home.MyTestMethod(1, 2).then(function (data) { alert(data); });
        JsActions.Home.MyTestMethod2(4, 5).then(function (data) { alert(data); });
        JsActions.Home.Yusuf(67, 9).then(function (data) { alert(data.toString()); });
        var ret = JsActions.Home.GetStudent(6);

        alert(ret.firstName);

        console.log(ret.firstName);

    })

But i can not access firstname. i see some code form codeplex:

enter image description here

Upvotes: 1

Views: 1034

Answers (1)

HaBo
HaBo

Reputation: 14297

 $(document).ready(function () {
        //JsActions.Home.MyTestMethod(1, 2).then(function (data) { alert(data); });
        //JsActions.Home.MyTestMethod2(4, 5).then(function (data) { alert(data); });
        //JsActions.Home.Yusuf(67, 9).then(function (data) { alert(data.toString()); });
// old var ret = JsActions.Home.GetStudent(6)
// new one
        var ret = JsActions.Home.GetStudent(6).responseText;

        alert(ret.firstName);

        console.log(ret.firstName);

    })

Note: your controller just speaks about GetStudent method so I commented other method calls.

Upvotes: 1

Related Questions