你好世界的淘汰Mvc教程不起作用

时间:2014-12-30 11:30:23

标签: knockout-mvc

我正在关注knockoutmvc的this教程。以下是我的代码。

查看:cshtml

@using PerpetuumSoft.Knockout
@model DynamicRowAdd.Models.HelloWorldModel

@{
    var ko = Html.CreateKnockoutContext();
}

@{
    ViewBag.Title = "View";
}

<h2>View</h2>

<p>First name: @ko.Html.TextBox(Model1=>Model1.FirstName)</p>
<p>Last name: @ko.Html.TextBox(Model1=>Model1.LastName)</p>
<h2>Hello, @ko.Html.Span(Model1=>Model1.FullName)!</h2>
@ko.Apply(Model)

型号:

using DelegateDecompiler;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace DynamicRowAdd.Models
{
    public class HelloWorldModel
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        [Computed]
        [ScriptIgnore]
        [JsonIgnore]
        public string FullName
        {
            get { return FirstName + " " + LastName; }
        }
    }
}

控制器:

using DynamicRowAdd.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PerpetuumSoft.Knockout;

namespace DynamicRowAdd.Controllers
{
    public class HelloWorldController : Controller
    {
        // GET: HelloWorld
        public ActionResult Index()
        {
            return View(new HelloWorldModel
            {
                FirstName = "Steve",
                LastName = "Sanderson"
            });
        }
    }
}

这里,当我运行代码时,我应该得到输出,例如教程中提到的输出。但我没有得到输出,因为它在教程中。相反,我在firstname,lastname和fullname中显示空白。

我可能做错了什么。?

1 个答案:

答案 0 :(得分:1)

示例缺少设置的一个关键部分:您必须在布局/视图中包含必要的javascript文件才能使Knockout和knockoutmvc正常工作。

QuickStart部分

中对此进行了描述
  

添加指向下一个js文件的链接:

<script src="@Url.Content("~/Scripts/jquery-x.y.z.min.js")" type="text/javascript"></script> <!-- Use your version of jQuery -->
<script src="@Url.Content("~/Scripts/knockout-x.y.z.js")" type="text/javascript"></script> <!-- Use your version of knockout -->
<script src="@Url.Content("~/Scripts/knockout.mapping-latest.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/perpetuum.knockout.js")" type="text/javascript"></script>