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);
}
}
您有链接到KO论坛讨论? – 2011-06-27 10:37:39
https://groups.google.com/forum/?hl=zh-CN#!forum/knockoutjs – 2011-06-27 12:17:11
谢谢...我的意思是实际的线程 - 虽然我知道论坛,但是在搜索时找不到讨论此问题的线索。你有链接吗?我有一些相关的问题,并且很想看看究竟究竟是什么。 – 2011-06-28 09:24:54