Click here to Skip to main content
13,147,383 members (33,041 online)
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 26 Dec 2013

Simplest knockoutJS and MVC4 Demo

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


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.


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:


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


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);

        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);


This is equipped with knockoutJS.

    ViewBag.Title = "KnockoutJs client";

<h2>KnockoutJs client</h2>

    <p>Name: <input data-bind='value: displayName' /></p> 
    <p>Age: <input data-bind='value: displayAge' /></p> 
    <p>Name Age: <input data-bind='value: displayNameAge' /></p> 
    <button data-bind='click: loadFromServer'>Load From Server</button>

        <p>Input Name: <input data-bind='value: inputName' /></p>
    <p>Input Age: <input data-bind='value: inputAge' /></p>
    <button data-bind='click: updateLocal'>Update Local</button>
    <button data-bind='click: updateServer'>Update Server</button>

    <h2>KO Content</h2>
    <pre data-bind="text: ko.toJSON($root, null, 2)"></pre>

<script src="~/Scripts/jquery-2.0.3.js"></script>
<script src="~/Scripts/knockout-3.0.0.js"></script>
<script type="text/javascript">
    var PersonViewModel = function(name, age) {
        var self = this;
        self.displayName = ko.observable(name);
        self.displayAge = ko.observable(age);
        self.displayNameAge = ko.computed(function() {
            return self.displayName() + " age " + self.displayAge();
        }, self);

        self.inputName = ko.observable("My Name");
        self.inputAge = ko.observable("15");
        self.updateLocal = function() {

        self.loadFromServer = function () {
            $.getJSON("/home/GetPerson", function (data) {

        self.updateServer = function() {
            var data = "{ \"name\" : \"" + self.inputName() + 
              "\", \"age\" : \"" + self.inputAge() + "\"}";
            $.post("/home/UpdatePerson", data, function(returnedData) {
            }, "json");

        self.updateThis = function(jsonData) {
            var jsonString = JSON.stringify(jsonData);
            var parsed = JSON.parse(jsonString);
    var myViewModel = new PersonViewModel("Jay Tan", "21");
    ko.applyBindings(myViewModel); // This makes Knockout get to work

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


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


About the Author

Jeson Martajaya
United States United States
Jeson Martajaya is a Senior Consultant for Magenic, a leading consulting firm in the US specializes in .NET and Mobile development. He has been professionally coding for more than 13 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 (OCA) in PL/SQL, Microsoft Certified Professional Developer (MCPD) in Windows Application, and two Microsoft Certified Solutions Developer (MCSD) in Web Development as well as in Application Lifecycle Management.

You may also be interested in...

Comments and Discussions

QuestionIt compiles :) Pin
Gluups11-Dec-15 2:35
memberGluups11-Dec-15 2:35 
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    Praise Praise    Rant Rant    Admin Admin   

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

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