asp.net-mvc-3 – 自动映射似乎不像’Name’字段
发布时间:2020-12-16 06:58:37 所属栏目:asp.Net 来源:网络整理
导读:我有一个看起来像这样的模型. class Aspect { Guid Id { get; set; } string Name { get; set; } string Description { get; set; } // multiple other properties} 在我的视图(ASP.NET MVC 3.0)中,我试图使用KnockoutJS映射插件.我这样称呼它. (下面列出的H
我有一个看起来像这样的模型.
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论坛上解决了这个问题.自动填充未在名称字段上触发更改事件.
定义了数据绑定,如:data-bind =“value:Name,valueUpdate:’blur’”,使其工作. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net核心身份提取并保存外部登录令牌并添加对本地身份的
- 在Entity Framework中使用存储过程(五):如何通过存储过程
- asp.net-mvc-3 – ASP.NET MVC 3,动作过滤器和Autofac依赖注
- Asp.Net MVC 5 Owin Twitter Auth throwing 401 Exception
- 如何在ASP.NET Core MVC 6中强制执行小写路由?
- ASP.Net数据库配置
- asp.net-mvc – MVC架构 – 重新使用相同的viewmodel进行读
- asp.net-mvc – MVC4脚手架添加控制器给出错误“无法检索元
- 为什么我不能在ASP.net MVC中使用服务器控件?
- asp.net-core-mvc – 如何防止Entity Framework Core 2.0重
推荐文章
站长推荐
- asp.net – NHibernate – 访问同一数据库的两个
- ASP.NET jQuery 实例13 原创jQuery文本框字符限制
- asp.net-mvc – 为什么@ Html.AntiForgeryToken(
- ASP.NET性能:没有预编译的Web应用程序
- ASP.NET:在URL中隐藏查询字符串
- asp.net-mvc – 将多个参数传递给控制器???
- asp.net – WCF与Web-api平台中的数据和事务
- asp.net-mvc – Excel Interop MVC
- asp.net-mvc – 防止更改隐藏字段
- asp.net – Team Build 2010和web.config转换的问
热点阅读