Return to Snippet

Revision: 53043
at November 11, 2011 04:12 by vtoman


Updated Code
// Get the json from the controller
function GetListItems() {
    $.ajax({
        type: "POST",
        url: "/JsonService/GetItems",
        contentType: "application/json; charset=utf-8",
        data: "{}",
        dataType: "json",
        success: function (result) {
            DisplayListItems(result);
        },
        "error": function (result) {
            var response = result.responseText;
            alert('Error loading: ' + response);
        }
    });
}

// Create list items and append them inside <ul> element
function DisplayListItems(list) {
    $.each(list, function(index, element) {
        var itemHTML = ["<li>",
                                "<div>",
                                    "<div>",
                                        element.Title,
                                    "</div>",
                                    "<div>",
                                        element.Description,
                                    "</div>",                                    
                                "</div>",
                            "</li>"].join('\n');
        $(".list > ul").append(itemHTML);
    }
}

// Controller method that serves json data
public JsonResult GetItems()
{
    IQueryable<Item> itemList = new DAO().GetList();

    return Json(from e in itemList
                select new
                {
                    Title = e.Title,
                    Description = e.Description
                });
}

Revision: 53042
at November 10, 2011 23:08 by vtoman


Updated Code
// Get the json from the controller
function GetListItems() {
    $.ajax({
        type: "POST",
        url: "/JsonService/GetItems",
        contentType: "application/json; charset=utf-8",
        data: "{}",
        dataType: "json",
        success: function (result) {
            DisplayListItems(result);
        },
        "error": function (result) {
            var response = result.responseText;
            alert('Error loading: ' + response);
        }
    });
}

// Create list items and append it inside <ul> element
function DisplayListItems(list) {
    $.each(list, function(index, element) {
        var itemHTML = ["<li>",
                                "<div>",
                                    "<div>",
                                        element.Title,
                                    "</div>",
                                    "<div>",
                                        element.Description,
                                    "</div>",                                    
                                "</div>",
                            "</li>"].join('\n');
        $(".list > ul").append(itemHTML);
    }
}

// Controller method that serves json data
public JsonResult GetItems()
{
    IQueryable<Item> itemList = new DAO().GetList();

    return Json(from e in itemList
                select new
                {
                    Title = e.Title,
                    Description = e.Description
                });
}

Revision: 53041
at November 10, 2011 22:54 by vtoman


Updated Code
function GetListItems() {
    $.ajax({
        type: "POST",
        url: "/JsonService/GetMediaCenterFiles",
        contentType: "application/json; charset=utf-8",
        data: "{}",
        dataType: "json",
        success: function (result) {
            DisplayListItems(result);
        },
        "error": function (result) {
            var response = result.responseText;
            alert('Error loading: ' + response);
        }
    });
}

function DisplayListItems(fileList) {
    $.each(fileList, function(index, file) {
        var fileItemHTML = ["<li>",
                                "<div class='mediaElement'>",
                                    "<div class='mediaTitle'>",
                                        fileList.Title,
                                    "</div>",
                                    "<div class='description'>",
                                        fileList.Description,
                                    "</div>",
                                    "<a href='",fileList.FileAddress,"'>",fileList.FileName,"</a>",
                                "</div>",
                            "</li>"].join('\n');
        $(".list > ul").append(fileItemHTML);
    }
}

private JsonResult GetSiteFiles(string parentDirectory)
{
    var mediaCenterFiles = new SiteFileDAO().ListSiteFilesByParentDirectory(parentDirectory);

    return Json(from mcf in mediaCenterFiles
                select new
                {
                    Title = mcf.FileTitle,
                    Description = mcf.Description,
                    FileName = GetSiteFileName(mcf.FilePath, parentDirectory),
                    FileAddress = mcf.FilePath
                });
}

Revision: 53040
at November 10, 2011 22:45 by vtoman


Initial Code
function GetListItems() {
    $.ajax({
        type: "POST",
        url: "/JsonService/GetMediaCenterFiles",
        contentType: "application/json; charset=utf-8",
        data: "{}",
        dataType: "json",
        success: function (result) {
            DisplayListItems(result);
        },
        "error": function (result) {
            var response = result.responseText;
            alert('Error loading: ' + response);
        }
    });
}

function DisplayListItems(fileList) {
    $.each(fileList, function(index, file) {
        var fileItemHTML = ["<li>",
                            "<div class='mediaElement'>",
                            "<div class='mediaTitle'>",
                            fileList.Title,
                            "</div>",
                            "<div class='description'>",
                            fileList.Description,
                            "</div>",
                            "<a href='",
                            fileList.FileAddress,
                            "'>",
                            fileList.FileName,
                            "</a>",
                            "</div>",
                            "</li>"].join('\n');
        $(".list > ul").append(fileItemHTML);
    }
}

private JsonResult GetSiteFiles(string parentDirectory)
{
    var mediaCenterFiles = new SiteFileDAO().ListSiteFilesByParentDirectory(parentDirectory);

    return Json(from mcf in mediaCenterFiles
                select new
                {
                    Title = mcf.FileTitle,
                    Description = mcf.Description,
                    FileName = GetSiteFileName(mcf.FilePath, parentDirectory),
                    FileAddress = mcf.FilePath
                });
}

Initial URL

                                

Initial Description

                                

Initial Title
Populating html list using Ajax call

Initial Tags
jquery

Initial Language
jQuery