Cie*_*iel 5 asp.net-mvc-3 knockout.js
我有一个看起来像这样的模型.
class Aspect {
Guid Id { get; set; }
string Name { get; set; }
string Description { get; set; }
// multiple other properties
}
Run Code Online (Sandbox Code Playgroud)
在我的视图(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;
Run Code Online (Sandbox Code Playgroud)
在大多数情况下,这实际上是有效的.然而,无论出于何种原因,它都不喜欢这个Name领域.
它会创造它,请注意.如果我postJson在knockout.js文件中放置一个断点,我可以坐在那里看看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 );
}
}
Run Code Online (Sandbox Code Playgroud)
看起来我们在KO论坛上解决了这个问题.自动填充未在名称字段上触发更改事件.
定义数据绑定就像: data-bind="value: Name, valueUpdate: 'blur'"使这个工作.
| 归档时间: |
|
| 查看次数: |
1489 次 |
| 最近记录: |