Click here to Skip to main content
11,639,554 members (62,279 online)
Click here to Skip to main content

Simplest knockoutJS and MVC4 Demo

, 26 Dec 2013 CPOL 23K 401 15
Rate this:
Please Sign up or sign in to vote.
MVC 4 knockoutJS project at bare minimum

Introduction

This is a C# MVC web project with Knockout JS at its bare minimum. It only contains MVC 4, KnockoutJS 3.0, and jQuery 2.0.3. Irrelevant packages are not added.

Background

I have been searching for the most simplistic example of MVC knockout web project. But unfortunately, I could not find any. Most of the examples are loaded with irrelevant extras. So I ended up spending a day writing and wiring up this simple demo. Feel free to download it.

The ingredients of this project are:

  1. Microsoft MVC 4.0
  2. KnockoutJS 3.0 - its backward compatible with 2.2.0
  3. jQuery 2.0.3. - should be compatible with jQuery 1.7

This project strives to be the most simplistic example of MVC-KnockoutJS wire up with the least number of files, so I am intentionally not having the following:

  • No Entity Framework (yes, you can have MVC without EF)
  • No KnockoutJS Mapping package
  • No irrelevant jQuery packages (jQueryUI, jQuery validation)
  • No CSS
  • No bundling nor modularizing
  • No Modernizr
  • No support for old browsers. I use Chrome for developing this.
  • No Injection container

Using the Code

This project uses MVC4 "Empty" project template and is written from the ground up. Only 3 files are manually written:

Model\Person.cs

namespace KnockoutJsonDemo.Models
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

Controller\HomeController.cs

using System.Web.Mvc;
using KnockoutJsonDemo.Models;
using Newtonsoft.Json;

namespace KnockoutJsonDemo.Controllers
{
    public class HomeController : Controller
    {
        private static Person _data = new Person()
        {
            Name = "PersonName",
            Age = 25
        };

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult TestJson()
        {
            return Json(new
                {
                    Name = "MyName",
                    Age = "17"
                }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetPerson()
        {
            return Json(_data, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult UpdatePerson()
        {
            string jsonString = Request.Form[0];
            Person person = JsonConvert.DeserializeObject<Person>(jsonString);
            _data.Name = person.Name;
            _data.Age = person.Age;
            return Json(_data);
        }
    }
}

View\Home\Index.cshtml

This is equipped with knockoutJS.

@{
    ViewBag.Title = "KnockoutJs client";
}

<h2>KnockoutJs client</h2>

I hope this small demo will help you. If you can make this example simpler, please let me know. Feedback is appreciated.

License

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

Share

About the Author

Jeson Martajaya
United States United States
Jeson Martajaya is a senior software engineer for CCC Information Services Inc, a leading software company in automotive collision industry. He has been professionally coding for more than 10 years, ranging from Java to Oracle to Windows and ASP.NET. He is enjoying it very much. He holds multiple professional credentials: Oracle Certified Associate in PL/SQL, Microsoft Certified Professional Developer in Windows Application, and two Microsoft Certified Solutions Developer in Web Development as well as in Application Lifecycle Management.

You may also be interested in...

Comments and Discussions

 
QuestionDownload Pin
paulg791127-Jan-15 3:13
memberpaulg791127-Jan-15 3:13 
AnswerRe: Download Pin
Jeson Martajaya30-Jan-15 7:35
memberJeson Martajaya30-Jan-15 7:35 
AnswerRe: Download Pin
Jeson Martajaya4-Feb-15 10:20
memberJeson Martajaya4-Feb-15 10:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150731.1 | Last Updated 26 Dec 2013
Article Copyright 2013 by Jeson Martajaya
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid