将动态元素添加到.NET MVC中已存在的模型中 [英] Add dynamic elements to an already existing model in .NET mvc

查看:64
本文介绍了将动态元素添加到.NET MVC中已存在的模型中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个仪表板(在.NET MVC项目中),该仪表板基本上是通过api接收数据并在(某种程度上)精美的视图中显示数据的.

I'm building a dashboard (in a .NET MVC project) that basically receives data through an api and displays it in a (somewhat) fancy view.

我收到的数据以json格式格式化,我知道"root"的格式(不确定json时这是否是正确的术语,请随时纠正我). >

The data I receive is formatted in json and I know the format of the "root" (not sure if this is the correct term when talking about json, feel free to correct me) which is something along those lines :

{
response : 
  {
  fields :
    [{
      name : name1,
     },
     {
      name : name2,
     },
     ...
    ],
  results :
    [{
      name1 : value1,
      name2 : value2
     },
     {
      name1 : value3,
      name2 : value4
     },
     ...
    ]
  }
}

如您所见,数组结果"从一个响应到下一个响应都不同,其中名称"name1"和"name2"是用户生成的(即不可预测的).现在,我可以使用此模型反序列化json来提取字段"数组及其值:

As you can see, the array 'results' varies from one response to the next, with the names 'name1' and 'name2' being user-generated (i.e. unpredictable). Now I can deserialize the json to extract the 'fields' array and its values using this model :

namespace WebApplication1.Models
{
    public class Field
    {
        public string name { get; set; }
    }

    public class Result
    {
    }
    public class JsonResponse
    {
        public List<Field> fields { get; set; }
        public List<Result> results { get; set; }
    }
}

我的问题是:

当我将其传递给视图时,结果"数组中填充了空的对象"(由于模型中未描述它们,因此感觉很正常)

when I pass this to the view, the 'results' array is filled with empty 'Objects' (which feels normal as they are not described in the model)

results: Array(5)
0: {}
1: {}
2: {}
3: {}
4: {}

我的目标是:

我希望能够在将结果"的正确描述(可以从字段"数组中获取)添加到模型之前,将其传递给视图,以便所有数据在视图中可用.

I want to be able to add the correct description of 'results' to the model (that I can get from the 'fields' array) before passing it to the view, so that all the data is available in the view.

尝试过的事情:

  1. 将Json作为字符串发送到视图并使用JavaScript进行解析,但这不起作用,因为我们使用依赖模型的引擎来显示数据

  1. sending the Json as a string to the view and parse it with JavaScript but that doesn't work since we use a model-dependant engine to display our data

使用dynamicObj解码json,但我无法将其传递给视图(因为它不是模型)

using a dynamicObj to decode the json, but I couldn't pass it to the view (as it is not a model)

正在哭泣,但是(令人惊讶的)它也不起作用.

crying, but it (surprinsingly) didn't work either.

这可能是我目前忘记的其他事情.

probably some other thing I'm forgetting at this time.

几天来我一直在网上寻找解决方案,但我感到绝望,欢迎任何想法.

I have been scouring the web for days for a solution, and I'm getting desperate, any ideas are welcome.

谢谢!

推荐答案

因此,对于以后寻找此解决方案的任何人,由于找不到解决方案,我最终使用了一种变通方法.

So, to anyone looking for this in the future, I ended up using a workaround as I didn't find a solution to this problem.

我没有动态模型,而是重新编写了"Result"类,如下所示:

Instead of having a dynamic model, I rewrote the 'Result' class as follow :

    public class Result
    {
        public List<KeyValuePair<string, string>> values { get; set; }
    } 

当我从API接收数据时可以填写.

that I can fill as I receive the data from the API.

这会使视图中的数据处理变得更加烦人,但实际上还不错.

It makes for slightly more annoying data handling in the view, but it's really not that bad.

P.s.我没有将其标记为答案,因为这只是一个不错的解决方法

P.s. I'm not marking this as answer as it is just a nice workaround

这篇关于将动态元素添加到.NET MVC中已存在的模型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆