传递空JS数组控制器发出空

编程入门 行业动态 更新时间:2024-10-10 19:21:42
本文介绍了传递空JS数组控制器发出空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下面是我的模型,它是一个WCF Proxy类

Here is my model which is a WCF Proxy class

public class MyModel { public Employees[] MyEmpls{get;set;} public int Id{get;set;} public OrgName{get;set;} }

传递与 MyEmpls下面一个JSON结构对象作为空数组 MVC的控制器。

["Id":12, "MyEmpls":[], "OrgName":"Kekran Mcran"]

控制器

[HttpPost] public ActionResult SaveOrg(MyModel model) { //model.MyEmpls is null here }

我期待 mode.MyEmpls 为空的C#阵列,而不是空。可能有人帮助我在这里?我们需要编写一个自定义模型粘合剂呢?

I am expecting mode.MyEmpls as empty c# array and not null. Could some one help me here? Do we need to write a custom model binder for this?

推荐答案

我认为一些其他的答案已经错过了这个问题的意思是:为什么默认的MVC模型绑定绑定一个空的JSON数组而不是空的空C#数组?

I think that some of the other answers have missed the meaning of the question: why does the default MVC model binder bind an empty Json array to null instead of an empty C# array?

好吧,我不能告诉你他们为什么这样做,但我可以告诉你它发生在哪里。对于MVC的源代码可以在这里codePLEX为:aspnetwebstack.$c$cplex/SourceControl/latest.你要找的文件是ValueProviderResult.cs在这里你可以看到:

Well, I can't tell you why they did that, but I can show you where it happens. The source for MVC can be found on CodePlex here: aspnetwebstack.codeplex/SourceControl/latest. The file you're looking for is ValueProviderResult.cs where you can see:

private static object UnwrapPossibleArrayType(CultureInfo culture, object value, Type destinationType) { if (value == null || destinationType.IsInstanceOfType(value)) { return value; } // array conversion results in four cases, as below Array valueAsArray = value as Array; if (destinationType.IsArray) { Type destinationElementType = destinationType.GetElementType(); if (valueAsArray != null) { // case 1: both destination + source type are arrays, so convert each element IList converted = Array.CreateInstance(destinationElementType, valueAsArray.Length); for (int i = 0; i < valueAsArray.Length; i++) { converted[i] = ConvertSimpleType(culture, valueAsArray.GetValue(i), destinationElementType); } return converted; } else { // case 2: destination type is array but source is single element, so wrap element in array + convert object element = ConvertSimpleType(culture, value, destinationElementType); IList converted = Array.CreateInstance(destinationElementType, 1); converted[0] = element; return converted; } } else if (valueAsArray != null) { // case 3: destination type is single element but source is array, so extract first element + convert if (valueAsArray.Length > 0) { value = valueAsArray.GetValue(0); return ConvertSimpleType(culture, value, destinationType); } else { // case 3(a): source is empty array, so can't perform conversion return null; } } // case 4: both destination + source type are single elements, so convert return ConvertSimpleType(culture, value, destinationType); } }

有趣的部分是案例3:

The interesting part is "case 3":

else { // case 3(a): source is empty array, so can't perform conversion return null; }

您可以通过初始化在它的构造模型阵列回避这个问题。在我的源快速阅读中,我不能告诉你为什么他们不能返回一个空数组或为什么他们决定不,但它应该为有趣的阅读。

You can sidestep this issue by initialising your array on the model in its constructor. In my quick reading of the source I can't tell you why they can't return an empty array or why they decide not to, but it should make for interesting reading.

更多推荐

传递空JS数组控制器发出空

本文发布于:2023-11-06 03:31:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562617.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   控制器   JS

发布评论

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

>www.elefans.com

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