Getting Started with ASP.NET MVC - Part 6: ASP.NET MVC and Entity Framework
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.


Upload Image Close it
Select File

Browse by Tags · View All
#DOTNET 33
#.NET 26
#ASP.NET 25
ASP.NET 24
brh 22
#C# 14
.NET 13
WCF 11
c# 8
#MultiThreading 7

Archive · View All
January 2011 10
September 2011 6
May 2011 6
December 2011 5
October 2011 5
June 2011 5
February 2011 3
April 2012 2
February 2012 2
November 2011 2

Client side Model binding with ASP.NET MVC 3 and KnockoutJs

Feb 7 2012 12:41AM by Neeraj Kaushik   

Client side Model binding with ASP.NET MVC 3 and KnockoutJs Knockoutjs is type safe client side library which provides declarative bindings of DOM with model data, Automatic UI refresh when view model changes etc. You can read more about KnockoutJS on its website. You can read more about AspNet MVC3 on Asp.Net MVC website.

Model

Create Model class on server side MarketPriceModel. its collection class MarketPriceCollectionModel.

public class MarketPriceCollectionModel
    {
        public List Prices { get; private set; }

        public void Add(MarketPriceModel model)
        {
            if (Prices == null)
                Prices = new List();
            Prices.Add(model);
        }
    }

    public class MarketPriceModel
    {
        public string Symbol { get; set; }
        public double Open { get; set; }
        public double Close { get; set; }
        public double Low { get; set; }
        public double High { get; set; }
        public DateTime TimeStamp { get; set; }
    }

Controller: MarketWatchController

This controller class has action method “MarketPriceUI” which return MarketPriceUI view and injecting MarketPriceCollectionModel class as model.

public class MarketWatchController : Controller
    {
        //
        // GET: /MarketWatch/

        public ActionResult MarketPriceUI()
        {
         MarketPriceCollectionModel collection = new MarketPriceCollectionModel();
         collection.Add(new MarketPriceModel() { Symbol = "MSFT", 
		Open = 34.5d, Close = 35.5d, Low = 33.4d, High = 36.5d });
         collection.Add(new MarketPriceModel() { Symbol = "Goog", 
		Open = 54.5d, Close = 58.65d, Low = 52.78d, High = 59.23d });

         return View(collection);
        }
    }
View MarketPriceUI.cshtml @model: contain object returns from controller. We can use this object to show data in view but here I want to convert @model to knockoutJS viewmodel.
            var initialData = @(Html.Raw(Json.Encode(Model.Prices)));

viewModel = {prices: ko.observableArray(initialData)
                };

           ko.applyBindings(viewModel);

@(Html.Raw(Json.Encode(Model.Prices))) converts model data into json and this data is set as observablearray for knockoutjs viewmodel.

“data-bind="foreach: viewModel.prices" statement can loop in this viewmodel to show data on page.

@model MarketWatchWebSample.Models.MarketPriceCollectionModel
@{
    ViewBag.Title = "MarketWatch";
    Layout = "~/Views/Shared/_Layout.cshtml";
}




<table>
    <tr>
        <td>
            Symbol
        </td>
        <td>
            High
        </td>
        <td>
            Low
        </td>
    </tr>
    <tbody data-bind="foreach: viewModel.prices">
        <tr>
            <td>
                <span data-bind="text:Symbol"></span>
            </td>
            <td>
                <span data-bind="text:High"></span>
            </td>
            <td>
                <span data-bind="text:Low"></span>
            </td>
        </tr>
    </tbody>
</table>

Output

image

Tags: 


Neeraj Kaushik
49 · 4% · 996
1
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

"Client side Model binding with ASP.NET MVC 3 and KnockoutJs" rated 5 out of 5 by 1 readers
Client side Model binding with ASP.NET MVC 3 and KnockoutJs , 5.0 out of 5 based on 1 ratings
    Copyright © Beyondrelational.com Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising