2011-06-04 32 views
5

我有一個看起來像這樣的模型。自動映射似乎不喜歡'名稱'字段

class Aspect { 
    Guid Id { get; set; } 
    string Name { get; set; } 
    string Description { get; set; } 
    // multiple other properties 
} 

在我的視圖(ASP.NET MVC 3.0)中我試圖使用KnockoutJS映射插件。我像這樣呼籲。 (HTML輔助上市下)

// attempt to bind any data we received from the server 
var serverData = @Html.Interpret(Model); 
// auto map the knockout attributes from the server data 
var viewModel = ko.mapping.fromJS(serverData); 
// apply the knockout binding to the viewModel 
ko.applyBindings(viewModel, $("#__frmAspect")[0]); 
// attach the jquery unobtrusive validator 
$.validator.unobtrusive.parse("#__frmAspect"); 

viewModel.Save = function() { 
    // we will try to send the model to the server. 
    ko.utils.postJson(
     $("#__frmAspect").attr('action'), { model: ko.toJS(viewModel) } 
    ); 
}; 

// bind the submit handler to unobtrusive validation. 
$("#__frmAspect").data("validator").settings.submitHandler = viewModel.Save; 

在大多數情況下,這種實際工作。但是,無論出於何種原因,它不喜歡Name字段。

創建它,介意你。如果我在knockout.js文件中的postJson處放置斷點,我可以坐在那裏看到ko.observable()確實存在。它只是沒有被輸入字段設置。

誰能告訴我爲什麼這可能是?

我的HTML輔助:

namespace System.Web.Mvc { 
    public static class KnockoutHelpers { 
     public static MvcHtmlString Interpret<TModel>(this HtmlHelper htmlHelper, TModel model) { 
      return new MvcHtmlString(model.ToJson()); 
     } 
    } 

    public static string ToJson (this object item) { 
     return new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(item); 
    } 
} 

回答

5

看起來我們拿到的這款解決的KO論壇。自動完成未在名稱字段上觸發更改事件。

定義了數據綁定,如:data-bind="value: Name, valueUpdate: 'blur'"來完成這項工作。

+0

您有鏈接到KO論壇討論? – 2011-06-27 10:37:39

+0

https://groups.google.com/forum/?hl=zh-CN#!forum/knockoutjs – 2011-06-27 12:17:11

+0

謝謝...我的意思是實際的線程 - 雖然我知道論壇,但是在搜索時找不到討論此問題的線索。你有鏈接嗎?我有一些相關的問題,並且很想看看究竟究竟是什麼。 – 2011-06-28 09:24:54

相關問題