首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MVC动态数据Webgrid

MVC动态数据Webgrid
EN

Stack Overflow用户
提问于 2015-05-18 23:15:29
回答 1查看 1.7K关注 0票数 0

我正在尝试在URL网格中显示旋转的数据,带有整齐的标题和某种类型的更改数据的复选框或可单击的数据中的URL

我用这个链接作为开始

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

我已经非常接近了,但是我似乎无法获得表中的数据,也无法创建链接。我都有两列,我想要一列

这是我的控制器

代码语言:javascript
复制
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);
    }

这是我在视图中所做的

代码语言:javascript
复制
@{
    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,我不确定如何提取它填充的值

问候

解决方案

EN

回答 1

Stack Overflow用户

发布于 2015-09-08 18:42:56

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

它的运行速度更快,也更简单

这是我的表格:

代码语言:javascript
复制
<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>

下面是我的控制器代码

代码语言:javascript
复制
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);
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30306914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档