ASP.NET MVC 4 JSON绑定到视图模型 – 嵌套对象错误
发布时间:2020-12-05 02:44:21 所属栏目:asp.Net 来源:互联网
导读:我有json绑定到视图模型的问题.这是我的代码: 部分ViewModels(AddressViewModel有更多的属性): public class AddressViewModel{ [Display(Name = Address_Town, ResourceType = typeof(Resources.PartyDetails))] public st
我有json绑定到视图模型的问题.这是我的代码: 部分ViewModels(AddressViewModel有更多的属性): public class AddressViewModel { [Display(Name = "Address_Town",ResourceType = typeof(Resources.PartyDetails))] public string Town { get; set; } [Display(Name = "Address_Country",ResourceType = typeof(Resources.PartyDetails))] public Country Country { get; set; } } public class Country : EntityBase<string> { public string Name { get; set; } protected override void Validate() { if (string.IsNullOrEmpty(Name)) { base.AddBrokenRule(new BusinessRule("CountryName","Required")); } } } 使用Javascript: $(document).on("click","#addAddress",function () { var jsonData = { "Town": $('#txt-Town').val(),"District": $('#txt-District').val(),"Street": $('#txt-Street').val(),"PostCode": $('#txt-PostCode').val(),"FlatNumber": $('#txt-FlatNumber').val(),"PremiseName": $('#txt-PremiseName').val(),"PremiseNumber": $('#txt-Premisenumber').val(),"Country": { "Name": $('#txt-Country').val(),} }; var addressData = JSON.stringify(jsonData); $.ajax({ url: '/Customer/SaveAddress',type: "POST",dataType: "json",contentType: "application/json; charset=utf-8",data: addressData,success: function (result) { $("#addIndividualAddressDialog").data("kendoWindow").close(); },error: function (result) { alert("Failed"); } }); }); 控制器负责人: [HttpPost] public ActionResult SaveAddress(AddressViewModel addressViewModel) 这是我用firebug看到的: 这就是我在VS看到的: 正如你所看到的,普通属性被绑定正确,但我的嵌套对象(国家)是空的.我读了很多不同的文章,我仍然不知道我做错了什么.请帮帮我! 解决方法问题来自于你的action方法参数:[HttpPost] public ActionResult SaveAddress(AddressViewModel addressViewModel) 当你使用JSON.stringify(),你发送一个字符串到您的控制器,而不是一个对象!所以,你需要做一些工作来实现你的目标: 1)更改您的动作方法参数: [HttpPost] public ActionResult SaveAddress(string addressViewModel) 2)将该字符串反序列化为一个对象 – 即AddressViewModel: IList<AddressViewModel> modelObj = new JavaScriptSerializer().Deserialize<IList<AddressViewModel>>(addressViewModel); 所以,你最后的行动方法应该如下: [HttpPost] public ActionResult SaveAddress(string addressViewModel) { IList<AddressViewModel> modelObj = new JavaScriptSerializer().Deserialize<IList<AddressViewModel>>(addressViewModel); // do what you want with your model object ... } (编辑:鄂州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 访问ASP.NET MVC应用程序中的控制器/操作列
- asp.net – Web Forms MVP项目有哪些好的资源?
- ASP.Net MVC 4窗体与2提交按钮/操作
- asp.net – 我可以愚弄HttpRequest.Current.Request.IsLoca
- asp.net-mvc – 从页面到局部视图的asp.net mvc参数
- .net – 有人有一个例子,说明为什么我会主持一个WCF服务
- asp.net-mvc-3 – 如何关闭我的整个ASP.NET MVC 3网站的缓存
- asp.net – 检查IE浏览器 – .NET
- asp.net-mvc-4 – 在EF迁移配置类的Seed方法中获取App_Data
- asp.net – 渗透测试人员说.ASPXAUTH cookie是不安全的并且
推荐文章
站长推荐
- asp.net – Request.Url.AbsoluteUri和重写的URL
- asp.net-mvc – 如何在ASP.NET MVC中使用单选模式
- asp.net-mvc – 无法更改关系,因为一个或多个外键
- asp.net-mvc – MVC DB首先修复显示名称
- asp.net – 如何使用Fiddler编辑HTTP请求
- asp.net 文件上传与刷新与asp.net页面与iframe之
- asp.net – 错误:数据绑定方法(如Eval(),XPath(
- 认证 – asp.net mvc 3:Page.User.IsInRole(“x
- asp.net-mvc-4 – 在一个项目中混合Web Api和ASP
- ASP.Net 2中的上传文件在哪里?
热点阅读