【问题标题】:How to allow users to create an arbitrary number of strings如何允许用户创建任意数量的字符串
【发布时间】:2015-11-29 03:29:06
【问题描述】:

我是 ASP.NET 的新手。我正在创建一个允许用户创建图表的简单网站。在前端,我使用的是 Charts.js,每个图表的数据如下所示:

var data = {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [
        {
            label: "My First dataset",
            fillColor: "rgba(220,220,220,0.2)",
            strokeColor: "rgba(220,220,220,1)",
            pointColor: "rgba(220,220,220,1)",
            pointStrokeColor: "#fff",
            pointHighlightFill: "#fff",
            pointHighlightStroke: "rgba(220,220,220,1)",
            data: [65, 59, 80, 81, 56, 55, 40]
        }
    ]
};

我的计划是为图表创建一个数据模型,允许用户指定图表的各种属性。下面是我的模型:

下面是我的课。假设每个图表现在只有一个数据集:

public class Chart
{
    public int ID { get; set; }
    public string title { get; set; }
    public string fillColor { get; set; }
    public string strokeColor { get; set; }
    public string pointColor { get; set; }
}

我的问题是,处理标签属性的最佳方法是什么?我是否应该将它作为另一个字符串属性包含在类中,让用户将每个标签输入到用分号分隔的输入框中,然后遍历它以填充 JavaScript 对象上的标签属性?还是有更好的办法?

注意:我不担心图表上的data 整数数组。它的所有起始值都是 0,所以我将对其进行硬编码。

【问题讨论】:

  • 它应该是一个list of string 属性。 public List<string> labels { get; set; }

标签: asp.net-mvc asp.net-mvc-5


【解决方案1】:

我希望保留如下模型。

   public class Dataset
    {
        public string label { get; set; }
        public string fillColor { get; set; }
        public string strokeColor { get; set; }
        public string pointColor { get; set; }
        public string pointStrokeColor { get; set; }
        public string pointHighlightFill { get; set; }
        public string pointHighlightStroke { get; set; }
        public List<int> data { get; set; }
    }

    public class Chart
    {
        public List<string> labels { get; set; }
        public List<Dataset> datasets { get; set; }
    }

【讨论】:

  • 谢谢。这两个类是在同一个模型 (.cs) 文件中还是分开有关系吗?
  • 是的,当然,我只是给出了核心模型,您可以根据您的要求添加。
  • 谢谢。最后一个问题:他们使用相同的 DBContext 吗?还是不一样?我假设由于数据集仅在图表内部创建/编辑,它们共享相同的 DBContext。
  • 它们应该共享相同的 DBcontext 。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-28
  • 1970-01-01
  • 2021-07-30
  • 1970-01-01
  • 2021-06-28
  • 2012-05-10
  • 1970-01-01
相关资源
最近更新 更多