【问题标题】:Draw2d C# MVC Post JSON to ControllerDraw2d C# MVC 将 JSON 发布到控制器
【发布时间】:2014-06-01 18:10:56
【问题描述】:

我正在使用允许导出 JSON 的 Draw2D 库,但是当我尝试这样做时,我无法将其导出到 MVC。这不是 Draw2D 的问题,而是我缺乏关于如何让它工作的知识。 我生成如下所示的 JSON

    [
    {
        "type": "DBTable",
        "id": "662b1fb8-5eb8-47a5-9f81-e48efb0d31bd",
        "x": 80,
        "y": 59,
        "width": 99,
        "height": 107,
        "cssClass": "DBTable",
        "bgColor": "#DBDDDE",
        "color": "#D7D7D7",
        "stroke": 1,
        "alpha": 1,
        "radius": 3,
        "name": "Default AutoAttendant",
        "entities": [
            {
                "text": "0",
                "id": "0c4c5414-1f35-4247-a4b7-38297aa0e5ff"
            },
            {
                "text": "1",
                "id": "706e1cb7-a9d1-461d-8230-0bf136c1d850"
            }
        ]
    },
    {
        "type": "NamedUser",
        "name": "Scott",
        "id": "0d2c71cf-52ee-4c50-a974-ea07003df05e",
        "cssClass": "NamedUser",
        "bgColor": "#DBDDDE",
        "color": "#D7D7D7",
        "stroke": 1,
        "alpha": 1,
        "radius": 3,
        "x": 220,
        "y": 200
    },
    {
        "type": "NamedUser",
        "name": "Nancy",
        "id": "601b0ad9-a0e9-4604-8861-38694a43e0a8",
        "cssClass": "NamedUser",
        "bgColor": "#DBDDDE",
        "color": "#D7D7D7",
        "stroke": 1,
        "alpha": 1,
        "radius": 3,
        "x": 220,
        "y": 200
    }
]

我的控制器目前非常基本,可以清除所有潜在的副作用问题。

public ActionResult Draw2dRetrieveJSON(AllJson AllJsontxt)
    {

我的班级是这样的

public class AllJson
{
   public  IEnumerable<HostedVoiceUserJson> HostedVoiceUserJson { get; set; }
   //public IEnumerable<HostedVoiceHuntGroupJson> HostedVoiceHuntGroupJson { get; set; }
   //public IEnumerable<HostedVoiceAAJson> HostedVoiceAAJson { get; set; }
   //public IEnumerable<HostedVoiceConnectionJSON> HostedVoiceConnectionJSON { get; set; }
}

我关注了这篇文章,它让我自己发布“NamedUser”对象并手动更改 JSON 字符串以匹配

{
                "HostedVoiceUserJson": [
                    {
                        "type": "NamedUser",
                        "name": "Scott",
                        "id": "0d2c71cf-52ee-4c50-a974-ea07003df05e",
                        "cssClass": "NamedUser",
                        "bgColor": "#DBDDDE",
                        "color": "#D7D7D7",
                        "stroke": 1,
                        "alpha": 1,
                        "radius": 3,
                        "x": 220,
                        "y": 200
                    },
                    {
                        "type": "NamedUser",
                        "name": "Nancy",
                        "id": "601b0ad9-a0e9-4604-8861-38694a43e0a8",
                        "cssClass": "NamedUser",
                        "bgColor": "#DBDDDE",
                        "color": "#D7D7D7",
                        "stroke": 1,
                        "alpha": 1,
                        "radius": 3,
                        "x": 220,
                        "y": 200
                    }
                ]
            }

当然,我不能一直坐在这里手动更改这些,我需要能够找到让它工作的地方,以便我可以将多种不同的类型一起发布回来。

【问题讨论】:

  • 我找到了解决方法,但这只是一种解决方法,它确实解决了我当前的问题,但由于这个问题是一个更广泛的问题,我认为它会为很多人服务很高兴获得比我所拥有的更彻底的解决方案,我保持这个状态。就我的解决方案而言,我创建了一个包含所有类的所有属性的新类,在它位于控制器中之后,我将它们与那里分开。不是最佳的,但目前可以使用。

标签: asp.net-mvc json draw2d draw2d-js


【解决方案1】:

看起来您的 JSON 是一个对象数组。您需要同质的 JSON 集合才能被 ModelBinder 自动识别。

根据类型,您可以分离 JSON 对象并将它们发送到不同的同类数组中。这样你就可以在 C# 中匹配你的模型类

【讨论】:

    猜你喜欢
    • 2017-02-22
    • 2017-06-22
    • 2012-10-03
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    相关资源
    最近更新 更多