asp.net-mvc – 从/到POCO对象的knockoutjs映射
有没有办法从/到POCO和knockoutjs映射?
我有一个Note类: public class Note { public int ID { get; set; } public string Date { get; set; } public string Content { get; set; } public string Category { get; set; } public string Background { get; set; } public string Color { get; set; } } 这是我的javascript: $(function () { ko.applyBindings(new viewModel()); }); function note(date,content,category,color,background) { this.date = date; this.content = content; this.category = category; this.color = color; this.background = background; } function viewModel () { this.notes = ko.observableArray([]); this.newNoteContent = ko.observable(); this.save = function (note) { $.ajax({ url: '@Url.Action("AddNote")',data: ko.toJSON({ nota: note }),type: "post",contentType: "json",success: function(result) { } }); } var self = this; $.ajax({ url: '@Url.Action("GetNotes")',type: "get",async: false,success: function (data) { var mappedNotes = $.map(data,function (item) { return new note(item.Date,item.Content,item.Category,item.Color,item.Background); }); self.notes(mappedNotes); } }); } 忽略不使用保存功能(简化此处的代码)的事实. 所以,当我加载页面时,我调用服务器,我检索一个Note对象的列表,并将其映射到javascript.请注意,如果ID不被映射,因为我不需要它. 到目前为止这么好,我看到屏幕上的笔记,但我如何可以将笔记保存回服务器? 我试图将注释(Im只保存新笔记而不是整个集合)转换为JSON并将其发送到我的控制器,但我不知道如何访问控制器中的注释.我试过了: public string AddNote(string date,string content,string category,string background,string color) { // TODO } 但不行.我想有一些像: public string AddNote(Note note) {} (Btw,在数据库中保存数据的方法的最佳回报是什么?void?) 那么我该怎么做呢?我尝试了knockout.mapping插件,但它是相当混乱,我不会让它为我工作. 谢谢. 解决方法
ASP.NET MVC的模型绑定器将查找区分大小写的属性.您需要将JSON对象传回给具有与您的poco对象匹配的属性名称的服务器.
我通常做2件事情之一: >使我的javascript对象属性名称大写(这样在JS中,我知道这个对象将在某个时候是服务器的DTO) function Note(date,background) { this.Date = date; this.Content = content; this.Category = category; this.Color = color; this.Background = background; }; >在我的AJAX调用中,我将创建一个匿名对象来传回给服务器(注意这不需要ko.toJSON): $.ajax({ url: '@Url.Action("AddNote")',data: JSON.stringify({ note: { Date: note.date,Content: note.content,Category: note.category,Color: note.color,Background: note.background } }),contentType: "application/json; charset=utf-8",success: function(result) { } }); (注意不同的contentType参数) 您将要使ActionMethod成为(注释),而不仅仅是参数数组. 另外,因为模型绑定器以几种不同的方式查看已发布的值.我已经运气发布JSON对象,指定了ActionMethod参数名称: { note: { Date: note.date,Background: note.background } } 做就是了: { Date: note.date,Background: note.background } (但是可以使用数组绑定到集合和复杂类型…等) 对于一个执行db调用的方法返回的“最佳”签名,我们通常更喜欢看到布尔值,但这也取决于您的需求.显然,如果它是微不足道的数据,则void将会很好,但是如果它更重要,则可能需要中继一个布尔值(至少),让您的客户端知道它可能需要重试(特别是如果有并发异常) . 如果您真的需要让您的客户知道数据库中发生了什么,您可以进入custom error handling和exception catching的世界. 另外,如果您需要根据成功/不成功的数据库提交向用户显示非常具体的信息,那么您可以根据数据库事务中发生的情况,查看创建重定向到某些视图的custom ActionResults. 最后,从服务器获取数据并使用Knockout … >如果您的属性名称相同,或者创建稍微更明确的映射,映射插件将再次工作 function Note(values){ //values are what just came back from the server this.date; this.content; this.category; this.color; this.background; initialize(values); //call the prototyped function at the bottom of the constructor }; Note.prototype.initialize = function(values){ var entity = this; //so we don't get confused var prop = ''; if (values) { for (prop in values) { if (values.hasOwnProperty(prop.toLowerCase()) && entity.hasOwnProperty(prop.toLowerCase())) { //the setter should have the same name as the property on the values object if (typeof (entity[prop]) === 'function') { entity[prop](values[prop]); // we are assuming that the setter only takes one param like a Knockout observable() } else {// if its not a function,then we will just set the value and overwrite whatever it was previously entity[prop] = values[prop]; } } } } }; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – 在另一个文件夹中捆绑的Javascript
- asp.net – ApplicationInsight导致网站在启动时挂起
- asp.net – 身份使用Web API授权属性角色
- 给微软的依赖注入框架写一些扩展方法
- ASP.NET WebApi:(405)方法不允许
- asp.net-mvc – 区域在ASP.NET视图?
- asp.net – ExecuteReader需要时间,而不是SQL服务器?
- asp.net – 为什么需要Server.HtmlEncode?
- asp.net-web-api – 如何创建角色并将用户添加到ASP.NET MV
- asp.net – NHibernate – ManagedWebSessionContext和WebS
- 如何在ASP.NET应用程序中记录错误(异常)?
- ASP.NET MVC3:部分视图和主视图之间的交互
- 如何自定义Asp.net Identity 2用户名已经采取验证
- asp.net – Controller.Execute呈现为没有内容类
- asp.net 无刷新分页实例代码
- asp.net-mvc-3 – 定义WebActivator.PreApplicat
- asp.net-mvc-4 – ClaimsAuthorizationManager.C
- .net – 任何具有Postsharp生产经验的人?
- asp.net-mvc – MvcContrib.MVC3-ci和Mvc.Contri
- asp.net – BC30560:’ExtensionAttribute’在名