【问题标题】:MVC Dynamic data WebgridMVC 动态数据 Webgrid
【发布时间】:2015-09-08 18:42:56
【问题描述】:

我正在尝试在 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 新手,我很欣赏这是繁琐的,我有点超出我的深度,但欢迎任何建议

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

问候

ol

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-09
      • 2015-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-25
      • 2016-01-17
      相关资源
      最近更新 更多