2017-10-05 47 views
0

我使用MVC-5和C#動態添加列到Web網格,當網格呈現一切看起來很好但列標題不會作爲操作鏈接。有兩個版本的代碼,都呈現但不作爲動作鏈接。 謝謝你的幫助。MVC-5 WebGrid添加列標題作爲操作鏈接

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 Links以外,一切似乎都正確呈現。我也嘗試了第二塊代碼,結果相同。 謝謝你的幫助。 我意識到看到整個視圖將是至關重要的,因爲我正在動態地構建網格。

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

回答

0

我發現了這個問題。我忘了@ grid.GetHtml上的列:cols。 謝謝