变量列的MVC网格视图

时间:2016-04-28 14:45:59

标签: asp.net-mvc

我希望显示一组包含多列数据的数据。列是可变的,可以根据从数据库中检索的数据进行更改。以下是片段:

------------类---------

public class gridmatx
{  public Grid Grid { get; set; }
}
public class Grid
{  public List<string> Columns { get; set; }
    public List<GridRow> Rows { get; set; }
}
public class GridRow
{  public string Label { get; set; }
    public List<decimal> Values { get; set; }
    public List<GridRow> ChildRows { get; set; }
}

---------控制器--------------

   public ActionResult Index1()
    {   gridmatx g = new gridmatx();
        g.Grid = new Grid();
        g.Grid.Columns = new List<string> { "2011", "2012", "2013", "2014", "2015" };
        g.Grid.Rows = new List<GridRow>
        {   new GridRow { Label = "First", Values = new List<decimal> { 5M, 2M, 1M, 8M, 10M } },
            new GridRow { Label = "Second", Values = new List<decimal> { 2M, 2M, 4M, 8M, 9M } },
            new GridRow { Label = "Third", Values = new List<decimal> { 12M, 8M, 1M, 9M, 3M } }
        };
        return View(g);
   }

------------查看---------

@model myproj.Models.gridmatx

@using GridMvc.Html
@{
ViewBag.Title = "Grid data ";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<head>
<meta name="viewport" content="width=device-width" />
<link href="@Url.Content("~/Content/Gridmvc.css")" rel="stylesheet" />
<link href="@Url.Content("~/Content/bootstrap.min.css")" rel="stylesheet" />
<script src="@Url.Content("~/Scripts/jquery-1.9.1.min.js")"></script>
<script src="@Url.Content("~/Scripts/gridmvc.min.js")"></script>
</head>
<body>
<h1>Grid.MVC</h1>
@Html.Grid(Model.Grid.Rows).Columns(columns =>
{   columns.Add(r => r.Label).Titled("Type");
    for (int i = 0; i < Model.Grid.Columns.Count; i++)
    {   var index = i;
        string value1 = Model.Grid.Columns[i];
        columns.Add(r => r.Values.ToArray()).Titled(value1);
    }
  }  )
 </body>
 </html>

使用r =&gt; r.Values.ToArray()导致错误。我不知道如何列出变量列。 我使用了链接中的示例:https://gridmvc.codeplex.com/wikipage?title=Custom%20columns用于telerik。 我将不胜感激。

0 个答案:

没有答案