MVC动态数据Webgrid

本文关键字:Webgrid 数据 动态 MVC | 更新日期: 2023-09-27 17:49:39

我正在尝试在webgrid中显示pivot数据,具有整洁的标题和某种类型的更改数据的复选框,或可单击的数据中的URL

我用这个链接作为开始

在c#中解析动态SQL,在ASP中绑定到WebGrid。净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新手,我知道这是很繁琐的,我有点超出我的深度,但任何建议都是欢迎的

我的具体问题是……我无法让Link列填充item。价值(我真的很想把"项目"。值"而不是"*",因为这似乎是正确的),我认为是因为它是ExpandoObject,我不确定如何拉出它的值填充

ol

MVC动态数据Webgrid

最后我放弃了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);
    }