Click here to Skip to main content
15,885,216 members
Articles / Web Development / HTML

MVC Techniques with jQuery, JSON, Knockout, and C#

Rate me:
Please Sign up or sign in to vote.
4.94/5 (138 votes)
2 Jan 2012CPOL14 min read 435K   22.4K   415  
Developing an Order Entry application with MVC.
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Web.Mvc;

namespace NorthwindWebControls
{
    public class PageIDGeneration
    {
        public string PageID = string.Empty;
        public string JQueryPageID = string.Empty;

        public List<PageCollection> pageCollection;

        /// <summary>
        /// Page ID Generation
        /// </summary>
        public PageIDGeneration()
        {
            PageID = Guid.NewGuid().ToString();
            JQueryPageID = "#" + PageID;
            pageCollection = new List<PageCollection>();
        }

        /// <summary>
        /// Generate ID
        /// </summary>
        /// <param name="objectName"></param>
        /// <returns></returns>
        public string GenerateID(string objectName)
        {                      
            PageCollection pageItem = new PageCollection();

            pageItem.PageID = PageID;
            pageItem.JQueryPageID = "#"+PageID;
            pageItem.ID = objectName;
            pageItem.FullID = PageID + objectName;
            pageItem.JQueryID = "#" + PageID +  objectName;
            pageCollection.Add(pageItem);

            return pageItem.FullID;
        }

        /// <summary>
        /// Generate JQuery
        /// </summary>
        /// <returns></returns>
        public List<PageCollection> GenerateJQuery()
        {
            return pageCollection;
        }

        public string RenderJavascriptVariables(string prefix)
        {
            StringBuilder js = new StringBuilder();

            js.Append("<script language=\"javascript\" type=\"text/javascript\">");
            js.Append(" var " + prefix + "PageID" + " = \"" + JQueryPageID + "\";");
          
            foreach (PageCollection item in pageCollection)
            {
                js.Append(" var " + prefix + item.ID + " = \"" + item.JQueryID + "\";");
            }

            js.Append("</script>");
            return js.ToString();

        }

    }

    public class PageCollection
    {                      
        public string JQueryPageID { get; set; }
        public string PageID { get; set; }
        public string ID { get; set; }
        public string FullID { get; set; }
        public string JQueryID { get; set; }
    }
   
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Joey Software Solutions
United States United States
Mark Caplin has specialized in Information Technology solutions for the past 30 years. Specializing in full life-cycle development projects for both enterprise-wide systems and Internet/Intranet based solutions.

For the past fifteen years, Mark has specialized in the Microsoft .NET framework using C# as his tool of choice. For the past four years Mark has been implementing Single Page Applications using the Angular platform.

When not coding, Mark enjoys playing tennis, listening to U2 music, watching Miami Dolphins football and watching movies in Blu-Ray technology.

In between all this, his wife of over 25 years, feeds him well with some great home cooked meals.

You can contact Mark at mark.caplin@gmail.com

...

Comments and Discussions