How to bind Knockout Observable in MVC Application

376 views Asked by At

I am Using Knockout with MVC and want to display the text which can be change at run time, My Application is showing me the data which I am giving in the coding section i.e. static data but as I change the data in textbox it's not providing any result.

My code is as follows:-

Model Class

using PerpetuumSoft.Knockout;
using PerpetuumSoft;
using DelegateDecompiler;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace MvcApplication20.Models
{
 public class Class1
{

    public string Buyer { get; set; }
    public string Car { get; set; }
    public string Price { get; set; }

    [Computed]
    public string user
    {
        get { return Buyer + " you buyed " + Car; }
        }
  }
}

Controller Class

using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication20.Models;
using PerpetuumSoft.Knockout;

namespace MvcApplication20.Controllers
{
 public class HomeController : Controller
{

    public ActionResult Index()
    { 
        return View(new Class1
        {
            Buyer = "Anubhav",
            Car = "Verna",
            Price = "12 lakh"
        });
}

}
}

View Class

<script src="~/Scripts/knockout-2.3.0.js"></script>
<script src="~/Scripts/knockout.mapping-latest.js"></script>
@using PerpetuumSoft.Knockout
@model MvcApplication20.Models.Class1   
@{
  ViewBag.Title = "Index";
  Layout = "~/Views/ViewPage1.cshtml";
  var ko = Html.CreateKnockoutContext();
}
 <p>Buyer name: @ko.Html.TextBox(m => m.Buyer)</p>
 <p>Car name: @ko.Html.TextBox(m => m.Car)</p>
 <h2>Hello, @ko.Html.Span(m => m.user)!</h2>

 @ko.Apply(Model)

It's showing me the values which are given in the coding but when changing at run time then it's not updating it's value.

0

There are 0 answers