0

I'm trying to display pivoted data in a webgrid, with tidy headers and some sort of either a checkbox that changes the data, or a URL in the data that is clickable

I used this link as a start

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

And I'm very nearly there, but I can't seem to get the data in the table as well as create a link. I have both in two columns and I want one column

here's my controller

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);
    }

and here's what I've done in the view

@{
    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")

and that runs ok, but there are two columns where i'd like one, with the value and the link

I'm a bit of a MVC novice and I appreciate that this is fiddly and I'm out of my depth a bit, but any advice is welcome

My specific question is...I can't get the Link column to populate with item.Value (I really want to put "item.Value" instead of "*" as that seems right) and I think is because it's ExpandoObject, I'm not sure how to pull out the value it's populating with

regards

ol

Community
  • 1
  • 1

1 Answers1

0

in the end I abandoned WebGrid and used a table

it works a lot faster and simpler

here's my table:-

<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>

and here's my Controller code

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);
    }