如何将JSON字符串化模型序列化为ASP.NET MVC3模型(How to serialize a JSON stringified model to an ASP.NET MVC3 model)

编程入门 行业动态 更新时间:2024-10-27 18:27:46
如何将JSON字符串化模型序列化为ASP.NET MVC3模型(How to serialize a JSON stringified model to an ASP.NET MVC3 model)

我在MVC 3网站上有以下片段。

模型:

public class Item1 { [Display(Name = "Family name")] public string FamilyName { get; set; } [Display(Name = "Given name")] public string GivenName { get; set; } }

更新1

感谢Darin我有一个部分解决方案,即删除item1. 在使用JavaScriptSerializer之前。 奇怪的是item1。 当使用强类型模型时,由Razor,MVC 3等添加。 无论如何,我现在有一个解决方案 - 谢谢。

更新2

在回答下面的Darin问题时,Item1是我模型中的一个实体。 该模型看起来像:

public class ItemModel { public Item1 item1 {get; set;} public Item2 item2 {get; set;} }

UPDATE 1

Thanks to Darin I have a partial solution which is to remove item1. before using JavaScriptSerializer as above. The strange thing is that item1. is added by Razor, MVC 3 etc when using a strongly typed model. Anyway I now have a solution - thanks.

UPDATE 2

In answer to Darin's question below, Item1 is one entity in my model. The model looks like:

public class ItemModel { public Item1 item1 {get; set;} public Item2 item2 {get; set;} }

The view uses @model ItemModel with the field as @Html.EditorFor(m => m.item1.FamilyName). The two entities are in different forms on the same screen and I am returning them as

var form_section1Data = JSON.stringify($("#form_section1").formParams()); //item1 var form_section1Data = JSON.stringify($("#form_section1").formParams()); //item2

and the Ajax call contains:

data: JSON.stringify({ form_section1: form_section1Data, form_section2: form_section2Data }),

I simplified the initial question (trying to be helpful :-))

最满意答案

这并不常见. 在属性名称中。 我们假设您有以下javascript对象:

var item = { familyName: 'Smith', givenName: 'Jane' };

以及以下控制器操作:

[HttpPost] public ActionResult SaveAsIs(Item1 model) { ... }

现在发送AJAX请求非常简单:

$.ajax({ url: '@Url.Action("SaveAsIs")', type: 'POST', contentType: 'application/json;charset=utf-8', data: JSON.stringify(item), success: function (result) { } });

这将在POST请求正文中发送以下有效内容:

{"familyName":"Smith","givenName":"Jane"}

如您所见,属性名称中没有点。 它们存在问题,因为ASP.NET MVC用于反序列化JSON请求的默认JavaScriptSerializer无法轻松配置为使用模型的不同属性名称。 如您所知,您不能在.NET对象的属性名称中包含点。

另一方面,如果您绝对需要在属性名称中包含此JSON有效负载,则可以使用JSON.NET编写custom value provider factory ,该custom value provider factory允许您使用可覆盖其名称的自定义属性来装饰视图模型属性。

It is not common to have . in property names. Let's suppose for example thta you have the following javascript object:

var item = { familyName: 'Smith', givenName: 'Jane' };

and the following controller action:

[HttpPost] public ActionResult SaveAsIs(Item1 model) { ... }

Now it's pretty easy to send an AJAX request:

$.ajax({ url: '@Url.Action("SaveAsIs")', type: 'POST', contentType: 'application/json;charset=utf-8', data: JSON.stringify(item), success: function (result) { } });

This will send the following payload in the POST request body:

{"familyName":"Smith","givenName":"Jane"}

As you can see there are no dots in the property names. They are problematic because the default JavaScriptSerializer that is used by ASP.NET MVC to deserialize JSON requests cannot be easily configured to use different property names for the model. And as you know, you cannot have a dot in the property name of a .NET object.

On the other hand if you absolutely need to have this JSON payload containing dots in the property names you could write a custom value provider factory using JSON.NET which allows you to decorate your view model properties with custom attributes that could override their names.

更多推荐

本文发布于:2023-07-15 05:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110698.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   字符串   序列   如何将   ASP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!