首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MVC-5 WebGrid将列标题添加为actionlink

MVC-5 WebGrid将列标题添加为actionlink
EN

Stack Overflow用户
提问于 2017-10-06 04:05:14
回答 1查看 1K关注 0票数 0

我使用MVC-5和C#向Web-grid动态添加列,当网格呈现时,一切看起来都很好,但是列标题不能作为动作链接。代码有两个版本,都是render,但不能用作Action链接。谢谢你的帮助。

代码语言:javascript
复制
    cols.Add(grid.Column(Model.DictionaryList[i].Keys.ElementAt(i).ToString(), Model.DictionaryList[i].Keys.ElementAt(i).ToString(),
                   format: @<Text>@Html.ActionLink((string)Model.DictionaryList[i].Values.ElementAt(i).ToString(), "Select_MSPart_Search",
                   "MaterialSupply", new
                   {
                       id = ViewData["id"],
                       type = 1,
                       SearchFilter = Model.DictionaryList[i].Keys.ElementAt(i).ToString(),
                       SearchValue = "",
                       rowsPerPage = 7
                   })</Text>, style: "column-action"));

     cols.Add(grid.Column(header: Model.DictionaryList[i].Keys.ElementAt(i).ToString(),columnName:  Model.DictionaryList[i].Keys.ElementAt(i).ToString(),
                                format: (item) => new HtmlString(Html.ActionLink((string)Model.DictionaryList[i].Values.ElementAt(i).ToString(), "Select_MSPart_Search",
                                "MaterialSupply", new
                                {
                                    id = ViewData["id"],
                                    type = 1,
                                    SearchFilter = Model.DictionaryList[i].Keys.ElementAt(i).ToString(),
                                    SearchValue = "",
                                    rowsPerPage = 7
                                },null).ToHtmlString()), style: "column-action"));

除了列标题不是我所希望的Action链接之外,一切似乎都正确呈现。我还尝试了第二段代码,得到了相同的结果。谢谢你的帮助。我意识到看到整个视图将是至关重要的,因为我正在动态地构建网格。

代码语言:javascript
复制
    @using System.Dynamic
@using System.Linq
@using System.Data.Common
@using CeleroPortal.Models.ConstraintsAnalysis
@using System.Collections.Generic
@using System.Web.Mvc.Html
@using System.Web.Mvc.Razor
@model mdlViews
@using PagedList.Mvc
@{
    ViewBag.Title = "View Report records Page";
    var result = new List<dynamic>();
    int rowcnt = 0;
    foreach (var recRow in Model.DlList)
    {
        var row = (IDictionary<string, object>)new ExpandoObject();
        Dictionary<string, object> eachFieldRow = (Dictionary<string, object>)recRow;     //for when list was string,object
        foreach (KeyValuePair<string, object> keyValuePair in eachFieldRow)
        {
            row.Add(keyValuePair.Key, keyValuePair.Value);
        }
        result.Add(row);
        rowcnt++;
    }

    //WebGrid grid = new WebGrid(source: result, rowsPerPage: Model.PageSize, canPage: true, canSort: true, sortFieldName: Model.Sort, sortDirectionFieldName: Model.SortDir);
    WebGrid grid = new WebGrid(rowsPerPage: Model.PageSize, canPage: true, canSort: false, sortFieldName: Model.Sort, sortDirectionFieldName: Model.SortDir);
    List<WebGridColumn> cols = new List<WebGridColumn>();
    cols.Add(grid.Column("Order ID","Order ID"));
    cols.Add(grid.Column("Part ID", "Part ID"));
    cols.Add(grid.Column("Description", "Description"));
    cols.Add(grid.Column("Customer ID", "Customer ID"));
    cols.Add(grid.Column("Customer", "Customer"));
    cols.Add(grid.Column("Qty Due", "Quantity Due"));
    cols.Add(grid.Column("Target Date", "Target Date"));
    cols.Add(grid.Column("Days", "Days"));
    cols.Add(grid.Column("Final Date", "Final Date"));
    for (int i = 9; i < Model.DictionaryList[0].Count(); i++)
    {
        if (Model.DictionaryList[0].Keys.ElementAt(i).StartsWith("#"))
        {
            cols.Add(grid.Column(Model.DictionaryList[0].Keys.ElementAt(i).ToString(), Model.DictionaryList[0].Keys.ElementAt(i).ToString()));
        }
        else
        {
            //cols.Add(grid.Column(Model.DictionaryList[i].Keys.ElementAt(i).ToString(),header: Model.DictionaryList[i].Keys.ElementAt(i).ToString(),
            //   format: (Model) => Html.ActionLink((string)Model.DictionaryList[i].Values.ElementAt(i).ToString(), "Select_MSPart_Search",
            //   "MasterSupply",new { id = ViewData["id"] , type = 1, SearchFilter = Model.DictionaryList[i].Keys.ElementAt(i).ToString() , SearchValue = "",
            //   rowsPerPage = 7}),style: "column-action"));
            cols.Add(grid.Column(header:Model.DictionaryList[i].Keys.ElementAt(i).ToString(),columnName: Model.DictionaryList[i].Keys.ElementAt(i).ToString(),
                       format: @<Text>@Html.ActionLink((string)Model.DictionaryList[i].Values.ElementAt(i).ToString(), "Select_MSPart_Search",
                       "MaterialSupply", new
                       {
                           id = ViewData["id"],
                           type = 1,
                           SearchFilter = Model.DictionaryList[i].Keys.ElementAt(i).ToString(),
                           SearchValue = "",
                           rowsPerPage = 7
                       })</Text>, style: "column-action"));
            //cols.Add(grid.Column(header: Model.DictionaryList[i].Keys.ElementAt(i).ToString(),columnName: Model.DictionaryList[i].Keys.ElementAt(i).ToString(),
            //                        format: (item) => @Html.ActionLink((string)Model.DictionaryList[i].Values.ElementAt(i).ToString(), "Select_MSPart_Search",
            //                        "MaterialSupply", new
            //                        {
            //                            id = ViewData["id"],
            //                            type = 1,
            //                            SearchFilter = Model.DictionaryList[i].Keys.ElementAt(i).ToString(),
            //                            SearchValue = "",
            //                            rowsPerPage = 7
            //                        }, null)));
        }
    }
    grid.Bind(result, rowCount: Model.TotalRecords, autoSortAndPage: true);
    grid.Pager(WebGridPagerModes.All);
}

<style type="text/css">
    .webgrid-table {
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        font-size: 1.2em;
        width: 100%;
        display: table;
        border-collapse: separate;
        border: solid 1px #000066; /*#98BF21;*/
        background-color: white;
    }

        .webgrid-table td, th {
            border: 1px solid #000099; /*#98BF21;*/
            padding: 3px 7px 2px;
        }

    .webgrid-header {
        background-color: #b3d9ff; /*#A7C942;*/
        color: #FFFFFF;
        padding-bottom: 4px;
        padding-top: 5px;
        text-align: left;
    }

    .webgrid-footer {
    }

    .webgrid-row-style {
        padding: 3px 7px 2px;
    }

    .webgrid-alternating-row {
        background-color: #e6f2ff; /*#EAF2D3;*/
        padding: 3px 7px 2px;
    }

    .webgrid-selected-row {
        background-color: #ffff66; /*#EAF2D3;*/
        padding: 3px 7px 2px;
    }
</style>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<div class="jumbotron">
    <h1><img src="~/Images/Celero.png" /></h1>
    <p class="lead">Constraints Analysis Application Pushed Orders Grid</p>
    <div>
        @Html.ActionLink("Search for Part across reports",  "Select_MSPart", "MaterialSupply", new { id = ViewData["Id"].ToString(), type = ViewData["type"]},null)
    </div>
</div>
<div class="row">
    @using (Html.BeginForm("Views", "ConstraintsAnalysis", new { id = ViewData["id"], type = ViewData["type"] }, FormMethod.Post))
    {
        <div class="row">
            <div class="form-horizontal" style="float: left">
                <div class="col-md-4">
                    @if (Model.RwsPrPge == Model.TotalRecords)
                    {
                        <select id="rwsPrPge" name="rwsPrPge">
                            <option value="5">50</option>
                            <option value="0">All</option>
                        </select>
                    }
                    else
                    {
                        <select id="rwsPrPge" name="rwsPrPge">
                            <option value="0">All</option>
                            <option value="5">50</option>
                        </select>
                    }
                    <input type="submit" value="Rows/Page" />
                    @Html.Hidden("id", (object)ViewData["Id"].ToString())
                    @Html.Hidden("type", (object)ViewData["type"].ToString())
                </div>
                <div class="col-lg-4">
                    @*@Html.DropDownListFor(Model => Model.SearchFilter, new SelectList(ViewBag.SelectFilter, "Value", "Text"),
                        "Select Part for Search", new { @class = "form-control", @placeholder = "Search Filter" })*@
                    @Html.DropDownList("m", new SelectList(ViewBag.SelectFilter, "Value", "Text"), "Select Part")
                </div>
                <div class="col-lg-4" float="right">
                    &emsp;
                    <button type="submit" class="btn btn-success" value="m">
                        <span class="glyphicon glyphicon-search"></span>
                    </button>
                </div>
                <br /><br />
                <div class="col-md-4">
                    <select id="Export" name="Export">
                        <option value="00">No Export</option>
                        <option value="XX">Export to CSV</option>
                        @Html.EditorFor(model => model.DlList)
                        @Html.ValidationMessageFor(model => model.DlList)
                    </select>
                </div>
                <div class="col-md-4">
                    <button type="submit" class="btn btn-success" value="Export" href="/ConstraintsAnalysis/Views">
                        <span class="glyphicon glyphicon-export"></span>
                    </button>
                </div>
            </div>
        </div>
        if (Model != null)
        {
            <span>
                &nbsp;
                <span class="vam" style="float: left">
                    @Html.ActionLink("Report Selection Menu", "Index", "ConstraintsAnalysis")
                </span>
                &nbsp;
                <span class="vam">
                    @grid.GetHtml(tableStyle: "table table-bordered table-hover",
                            headerStyle: "webgrid-header",
                            footerStyle: "webgrid-footer",
                            alternatingRowStyle: "webgrid-alternating-row",
                            selectedRowStyle: "webgrid-selected-row",
                            caption: "Celero Constraints Grid",
                            rowStyle: "webgrid-row-style");
                </span>
            </span>
        }
    }
</div>
EN

回答 1

Stack Overflow用户

发布于 2017-10-10 02:40:14

我找到问题了。我忘记了列:@grid.GetHtml上的cols。谢谢

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46593928

复制
相关文章

相似问题

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