如何在MVC应用程序中绑定Knockout Observable

时间:2013-09-05 09:35:22

标签: knockout.js knockout-mvc

我正在使用Knockout和MVC并希望显示可以在运行时更改的文本,我的应用程序向我显示我在编码部分给出的数据,即静态数据,但是当我更改文本框中的数据时,它是没有提供任何结果。

我的代码如下: -

模型类

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

控制器类

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

}
}

查看课程

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

它显示了编码中给出的值,但在运行时更改时,它不会更新它的值。

0 个答案:

没有答案