MVC动态数据Webgrid

时间:2015-05-18 15:15:29

标签: c# asp.net-mvc

我正在尝试在webgrid中显示透视数据,包含整齐的标题和某种可更改数据的复选框,或者可点击的数据中的URL

我使用此链接作为开始

Parsing Dynamic SQL in C# and binding to WebGrid in ASP.Net MVC

而且我几乎就在那里,但我似乎无法获取表格中的数据以及创建链接。我有两列,我想要一列

这是我的控制器

public ActionResult GridIndex()
    {
        StoreDepartmentBusinessLayer sdc = new StoreDepartmentBusinessLayer();
        List<dynamic> PivotList = sdc.GetData();

        List<WebGridColumn> columns = new List<WebGridColumn>();
        columns.Add(new WebGridColumn() { ColumnName = "SapStoreID", Header = "Id",CanSort=true });
        columns.Add(new WebGridColumn() { ColumnName = "StoreName", Header = "Store", CanSort = true, Style = "webgrid-column-storename" });

        //List<Department> Departments = sdc.StoreDepartments.Select(x=>x.DeptID + " " + x.DeptName).Distinct().ToList();

        DepartmentBusinessLayer dbl = new DepartmentBusinessLayer();

        List<Department> Departments = dbl.Departments.Where(x => x.Active==1 && x.Calculation==0).ToList();

        foreach (Department LoopDept in Departments)
        {
            //columns.Add(new WebGridColumn() { ColumnName = LoopDept.DeptID.ToString(), Header = LoopDept.DeptName});
            char[] DeptVertical = LoopDept.DeptName.ToCharArray();
            string DeptVerticalString = string.Join(Environment.NewLine, DeptVertical);
            //this one works ok for 0 and 1
            columns.Add(new WebGridColumn() { ColumnName = LoopDept.DeptID.ToString(), Header = DeptVerticalString, Style = "webgrid-column-tick" });

            columns.Add(new WebGridColumn()
            {
                ColumnName = LoopDept.DeptID.ToString() + "_Link",
                Header = "",
                Style = "webgrid-column-tick",
                Format = (item) =>
                {
                    return new HtmlString(string.Format("<a href= {0}>{1}</a>",
                        Url.Action("Edit", "Edit", new { PseudoKey = item.SapStoreID + "_" + LoopDept.DeptID.ToString() }), "*"));
                }
            });

        ViewBag.Columns = columns;

        return View(PivotList);
    }

这就是我在视图中所做的事情

@{
    var grid = new WebGrid(Model, canPage: true, rowsPerPage: 100,
    selectionFieldName: "selectedRow", ajaxUpdateContainerId: "gridContent");
    grid.Pager(WebGridPagerModes.NextPrevious);
    }
@grid.GetHtml(tableStyle: "webgrid-tidy", headerStyle: "webgrid-header", columns: ViewBag.Columns,rowStyle: "webgrid-row-style",alternatingRowStyle: "webgrid-alternating-row")

并且运行正常,但有两列我喜欢一个,带有值和链接

我有点像MVC新手,我很欣赏这种情况很狡猾,而且我有点不自觉,但欢迎任何建议

我的具体问题是......我无法通过item.Value填充链接列(我真的想把“item.Value”而不是“*”,因为这似乎是正确的)我认为是因为它是ExpandoObject,我不知道如何提取其填充的值

问候

1 个答案:

答案 0 :(得分:0)

最后我放弃了WebGrid并使用了一个表

它的工作速度更快,更简单

这是我的表: -

<table class="table table-bordered">
@foreach (string LoopHeader in ViewBag.Columns)
{
    <th>@LoopHeader</th>
}
@foreach (var row in Model)
{
    <tr>
        @foreach (var column in row)
        {
            <td>
            @((column.Key == "SapStoreID" || column.Key == "StoreName") ?     column.Value : Html.ActionLink((string)column.Value.ToString(), "Edit",     "StoreDepartment", new { PseudoKey = row.SapStoreID + "_" + column.Key }, ""))
            </td>
            }
</tr>
}
</table>

这是我的控制器代码

public ActionResult GridIndex()
    {
        StoreDepartmentBusinessLayer sdc = new StoreDepartmentBusinessLayer();
        List<dynamic> PivotList = sdc.GetData();

        List<string> columns = new List<string>();
        columns.Add("Store ID");
        columns.Add("Store Name");

        DepartmentBusinessLayer dbl = new DepartmentBusinessLayer();
        List<Department> Departments = dbl.Departments.Where(x => x.Active==1 && x.Calculation==0).ToList();

        foreach (Department LoopDept in Departments)
        {
            char[] DeptVertical = LoopDept.DeptName.ToCharArray();
            string DeptVerticalString = string.Join(Environment.NewLine, DeptVertical);
            //this one works ok for 0 and 1
            columns.Add(DeptVerticalString);
        }
        ViewBag.Columns = columns;

        return View(PivotList);
    }