2015-05-07 27 views
1

我遵循本教程:http://knockoutmvc.com/GiftList創建可編輯列表。 但是當我運行代碼。它注意到了一個錯誤基因缺失錯誤

類型或命名空間名稱「PerpetuumSoft」找不到(你 缺少using指令或程序集引用?)

來自該行的錯誤:

@model MvcMovie.Models.GiftListModel 
@using PerpetuumSoft.Knockout //Error in this line 


@{ 
    ViewBag.Title = "Index"; 
} 
@{ 
    var ko = Html.CreateKnockoutContext(); //and also Error in this line 
} 

我包括在束敲除

 bundles.Add(new ScriptBundle("~/bundles/knockout").Include(
     "~/Scripts/knockout-2.1.0.js", 
     "~/Scripts/knockout-2.1.0.debug.js")); 

並添加共享佈局名稱這段代碼_Layout.cstml

@Scripts.Render("~/bundles/knockout") 

其實,我搜索這個教程,因爲我想創建一個視圖,其中用戶可以編輯列表,數據只能保存在用戶點擊按鈕保存,這意味着用戶可以做任何他們想做的事情。數據庫僅在啓動按鈕保存時更新。如果您知道其他方式而不使用任何擴展或插件,請推薦它(因爲我必須使用ASP .NET MVC 4,不添加任何擴展名)。非常感謝您。

這是鑑於

@model MvcMovie.Models.GiftListModel 
@using PerpetuumSoft.Knockout 


@{ 
    ViewBag.Title = "Index"; 
} 
@{ 
    var ko = Html.CreateKnockoutContext(); 
} 
<p>You have asked for @ko.Html.Span(m => m.Gifts.Count) gift(s)</p> 
<form id="myform"> 
    <table> 
    <tbody> 
     @using (var items = ko.Foreach(m => m.Gifts)) 
     { 
     <tr> 
      <td>Gift name:@items.Html.TextBox(item => item.Title, new { @class = "required" }).UniqueName()</td> 
      <td>Price: [email protected](item => item.Price, new { @class = "required number" }).UniqueName()</td> 
      <td>@ko.Html.Button("Delete", "RemoveGift", "GiftList", new { index = items.GetIndex() })</td> 
     </tr> 
     } 
    </tbody> 
    </table> 

    @ko.Html.Button("Add", "AddGift", "GiftList") 
    <button @ko.Bind.Enable(m => m.Gifts.Count > 0) type="submit">Save</button> 
</form> 
@using (ko.If(m => m.ServerTime.Length > 0)) 
{ 
    <p>Saved at @ko.Html.Span(m => m.ServerTime)</p> 
} 
<script type="text/javascript"> 
    $("#myform").ajaxForm(); 
    $("#myform").validate({ submitHandler: function() { @ko.ServerAction("Save", "GiftList"); } }); 
</script> 
<style scoped="scoped"> 
    input.error { 
    border: 1px solid red; 
    background-color: #FDC; 
    } 

    label.error { 
    display: block; 
    color: Red; 
    font-size: 0.8em; 
    } 
</style> 

@ko.Apply(Model) 

回答

0

那是因爲你缺少following Nuget package通過Perpetuumsoft代碼。這個包提供了該教程中使用的MVC挖空助手。

如果您不想使用該軟件包,請刪除using語句和那個var ko行。只要確保你applyBindings在你的視圖模型上的某個地方。

+0

這意味着我必須安裝:PerpetuumSoft.Knockout 0.5.7.2。你知道任何其他方式來解決我的問題 –

+0

@TranDuyLinh是的,無論最新的軟件包是什麼(儘管nuget會照顧你的)。您可以通過刪除這些Html幫助程序引用並自己編寫淘汰代碼來解決問題。 – mattytommo

+0

它是我第一次使用Knockout,請你詳細解釋一下 –