Mvc razor using JsonResult

Json has become one of the most popular form of data interchange method and being tightly integrated in the mvc framework makes it very easy to use. I will be demonstratating a very usefull technique of passing a response in an Ajax style using JsonResult.

Controller
public JsonResult GetLanguage()
{
    List<string> language = new List<string>();
    language.Add("c++");
    language.Add("java");
    language.Add("c#");
    language.Add("javaScript");

    return Json(language, JsonRequestBehavior.AllowGet);
}

View
In view call GetLanguage Method using jquery $.get method
$(function () {
    $.get("/Home/GetLanguage", function (data) {
        $.each(data, function (index, users) {
            $('ul').append($('<li>').append(users));
        });
    });
});
Mvc razor using JsonResult Mvc razor using JsonResult Reviewed by Bhaumik Patel on 5:34 AM Rating: 5