使用MVC 3滤波器输出到Excel [英] Filter Excel Output using MVC 3

查看:147
本文介绍了使用MVC 3滤波器输出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想过滤发送到一个Excel文件中的行。我已经能够弄清楚如何行数据导出到Excel,但现在我需要过滤的数据发送到因为我加了一个版本下拉一个Excel工作表。会发生什么事是我选择我想从下拉列表中看到的版本。

防爆。该版本是12.1。

该视图将仅带回具有与其相关联的版本号12.1数据。现在,当我想下载一个Excel文件中的过滤行。我preSS下载到excel文件按钮,并在一个excel文件打开该文件,除了它带来的所有行,而不是过滤行。我认为所有我需要做的就是添加相应viewbag和可视数据变量的呼叫,它会工作,没有这样的运气,它仍保持使每一行。难道你们看看我的code和帮助我找出我做错了吗?我已经发布了code下面请让我知道如果你需要更多信息,更好地诊断,我会经常回来看看!感谢您的帮助!

Excel的控制器

  [授权]
        公众的ActionResult PaExcelOutput(字符串的SortField,串currentFilter,字符串搜索字符串,INT?页,串VERS)
        {            INT PAGENUMBER =(第53页1); //启动页码            VAR PaExcel =从p在db.iamp_mapping
                          选择P;
            ViewBag.CurrentField = =的SortField String.IsNullOrEmpty(的SortField)? IAMP_PK:的SortField; //提供的字段进行排序
            ViewBag.CurrentFilter =搜索字符串; //提供与当前过滤字符串的看法            计算机[SelectedVersion] = VERS;
            计算机[currentFilter] = currentFilter;
            计算机[简称VER] =新的SelectList(PaExcel.Select(X => x.VERSION).Distinct());            如果(!String.IsNullOrEmpty排量(VERs))
            {
                PaExcel = PaExcel.Where(p值=方式> p.VERSION.ToUpper()包含(VERS));
            }            如果(Request.HttpMethod ==GET)
            {
                搜索字符串= currentFilter; //设置currentFilter等于搜索字符串
            }            其他
            {
                页面= 1; //默认为第1页
            }
            返回查看(PaExcel);
        }

输出到Excel视图

  @model IEnumerable的< D​​BFirstMVC.Models.iamp_mapping><! - 这就是MVC使用输出正确的行到Excel  - >@ {
    布局= NULL;
    Response.AddHeader(内容类型,应用程序/ vnd.ms - Excel中);
}
<表>
    &所述; TR>
        <第i个
            PA
        < /第i
        <第i个
            版
        < /第i
        <第i个
            投资领域
        < /第i
        <第i个
            重大项目
        < /第i
        <第i个
            PA子程序
        < /第i
        <第i个
            VP
        < /第i
        <第i个
            导向器
        < /第i
        <第i个
            温泉
        < /第i
        <第i个
            PA倡议
        < /第i
        <第i个
            笔记
        < /第i
        <第i个
            TEAM BC
        < /第i
        <第i个
            PA CC
        < /第i
        <第i个
            PA描述
        < /第i
        <第i个
            PA MANAGER
        < /第i
        <第i个
            SPA CC
        < /第i
        <第i个
            方法1
        < /第i
        <第i个
            方法2
        < /第i
        <第i个
            方法3
        < /第i
        <第i个
            TIER 4
        < /第i
        <第i个
            CFP VIEW NV / NONNV
        < /第i
        <第i个
            CFP VIEW
        < /第i
        <第i个
            创建日期
        < /第i
        <第i个
            CAF项目名称
        < /第i
        <第i个
            CAF IA
        < /第i
        <第i个
            CAF MP
        < /第i
        <第i个
            CAF LEAD DIR
        < /第i
        <第i个
            CAF LEAD MGR
        < /第i
        <第i个
            CAF CC DIR
        < /第i
        <第i个
            CAF CC MGR
        < /第i
        <第i个
            CAF CC
        < /第i
        <第i个
            CAF ID
        < /第i
        <第i个
            创建于
        < /第i
        <第i个
            最后编辑
        < /第i
        <第i个
            项目状态
        < /第i
        百分位>< /第i
    < / TR>@foreach(以型号VAR项){
    &所述; TR>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.PA)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.VERSION)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.INVESTMENT_AREA)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.MAJOR_PROGRAM)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.PA_SUB_PROGRAM)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.VP)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.DIRECTOR)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.SPA)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.PA_INITIATIVE)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.NOTES)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.TEAM_BC)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.PA_CC)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.PA_DESCRIPTION)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.PA_MANAGER)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.SPA_CC)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.TIER_1)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.TIER_2)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.TIER_3)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.TIER_4)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.CFP_VIEW_NV_NONNV)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.CFP_VIEW)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.CREATION_DATE)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.CAF_PROJECT_NAME)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.CAF_IA)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.CAF_MP)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.CAF_LEAD_DIR)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.CAF_LEAD_MGR)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.CAF_CC_DIR)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.CAF_CC_MGR)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.CAF_CC)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.CAF_ID)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.CREATED_ON)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.LAST_EDITED)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.PROJECT_STATUS)
        < / TD>    < / TR>
}< /表>

索引视图

 <! - 堆栈溢出,这就是ActionLink的是下载到Excel按钮位于它在叫PAExcelOutput--这个code段的底部>
@model PagedList.IPagedList< D​​BFirstMVC.Models.iamp_mapping>
@ {
    ViewBag.Title =PA映射;}@using PagedList;
< H2类=企业sIFR的> @ ViewBag.Title< / H>
< D​​IV CLASS =横条>
@using(Html.BeginForm())
{    < D​​IV CLASS =左撇子>
        通过筛选版本:@ Html.DropDownList(简称VER,NULL,全部,新{@class =文本})和标准:@ Html.TextBox(SearchString在,,新{@class = 文本})
    < / DIV>
    <输入类型=提交级=Button1的价值=转到/>
}< D​​IV CLASS =右倾>
    @ Html.ActionLink(添加新的PA到数据库,创建,霸,空,新的{@class =Button1的})< / DIV>
< / DIV>< D​​IV CLASS =横条>
    < D​​IV CLASS =左撇子>
    页@(Model.PageCount< Model.PageNumber 0:Model.PageNumber)
    动@ Model.PageCount
    &安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;
    @if(Model.Has previousPage)
    {
        @ Html.ActionLink(<<,,新页面{= 1,中将sortOrder = ViewBag.CurrentSort,currentFilter = ViewBag.CurrentFilter,VERS = ViewBag.SelectedVersion})
        @ Html.Raw();
        @ Html.ActionLink(< preV,,新的{页= Model.PageNumber - 1,中将sortOrder = ViewBag.CurrentSort,currentFilter = ViewBag.CurrentFilter,VERS = ViewBag.SelectedVersion})
    }
    其他
    {
        @:其中;&下;
        @ Html.Raw();
        @:或其可preV
    }    @if(Model.HasNextPage)
    {
        @ Html.ActionLink(下一步>中,新{页= Model.PageNumber + 1 =中将sortOrder ViewBag.CurrentSort,currentFilter = ViewBag.CurrentFilter,VERS = ViewBag.SelectedVersion})
        @ Html.Raw();
        @ Html.ActionLink(>>中,新{页= Model.PageCount,中将sortOrder = ViewBag.CurrentSort,currentFilter = ViewBag.CurrentFilter,VERS = ViewBag.SelectedVersion})
    }
    其他
    {
        @:下一个>
        @ Html.Raw()
        @:>>
    }
    < / DIV>
    < D​​IV CLASS =右倾>
        从@ Model.TotalItemCount记录显示记录@ Model.FirstItemOnPage为@ Model.LastItemOnPage
    < / DIV>
< / DIV>
<表>
    &所述; TR>
        <第i个
            @ Html.ActionLink(PA,,新的{中将sortOrder = ViewBag.currentOrder ==ASC DESC?:ASC=的SortFieldPA,VERS = ViewBag.SelectedVersion})
            @if(ViewBag.currentOrder ==ASC&放大器;&安培; ViewBag.CurrentField ==PA)
            {< IMG ID =ASCSRC =@ Url.Content(〜/图片/ ico_tablesortoffset_asc.gif),ALT =ASC箭/>}
            @if(ViewBag.currentOrder ==降序&放大器;&安培; ViewBag.CurrentField ==PA)
            {< IMG ID =降序SRC =@ Url.Content(〜/图片/ ico_tablesortoffset_desc.gif),ALT =说明箭/>}
        < /第i
        <第i个
            @ Html.ActionLink(重大项目,,新的{中将sortOrder = ViewBag.currentOrder ==ASC DESC?:ASC=的SortFieldMAJOR_PROGRAM,VERS = ViewBag.SelectedVersion})
            @if(ViewBag.currentOrder ==ASC&放大器;&安培; ViewBag.CurrentField ==MAJOR_PROGRAM)
            {< IMG ID =ASCSRC =@ Url.Content(〜/图片/ ico_tablesortoffset_asc.gif),ALT =ASC箭/>}
            @if(ViewBag.currentOrder ==降序&放大器;&安培; ViewBag.CurrentField ==MAJOR_PROGRAM)
            {< IMG ID =降序SRC =@ Url.Content(〜/图片/ ico_tablesortoffset_desc.gif),ALT =说明箭/>}
        < /第i
        <第i个
            @ Html.ActionLink(投资区,,新的{中将sortOrder = ViewBag.currentOrder ==ASC DESC?:ASC=的SortFieldINVESTMENT_AREA,VERS = ViewBag.SelectedVersion})
            @if(ViewBag.currentOrder ==ASC&放大器;&安培; ViewBag.CurrentField ==INVESTMENT_AREA)
            {< IMG ID =ASCSRC =@ Url.Content(〜/图片/ ico_tablesortoffset_asc.gif),ALT =ASC箭/>}
            @if(ViewBag.currentOrder ==降序&放大器;&安培; ViewBag.CurrentField ==INVESTMENT_AREA)
            {< IMG ID =降序SRC =@ Url.Content(〜/图片/ ico_tablesortoffset_desc.gif),ALT =说明箭/>}
        < /第i
        <第i个
            @ Html.ActionLink(版本,,新的{中将sortOrder = ViewBag.currentOrder ==ASC DESC?:ASC=的SortField版本,VERS = ViewBag.SelectedVersion})
            @if(ViewBag.currentOrder ==ASC&放大器;&安培; ViewBag.CurrentField ==版本)
            {< IMG ID =ASCSRC =@ Url.Content(〜/图片/ ico_tablesortoffset_asc.gif),ALT =ASC箭/>}
            @if(ViewBag.currentOrder ==降序&放大器;&安培; ViewBag.CurrentField ==版本)
            {< IMG ID =降序SRC =@ Url.Content(〜/图片/ ico_tablesortoffset_desc.gif),ALT =说明箭/>}
        < /第i
        <第i个
            @ Html.ActionLink(VP / SR主任。,,新的{中将sortOrder = ViewBag.currentOrder ==ASC DESC?:ASC=的SortFieldVP,VERS = ViewBag.SelectedVersion})
            @if(ViewBag.currentOrder ==ASC&放大器;&安培; ViewBag.CurrentField ==VP)
            {< IMG ID =ASCSRC =@ Url.Content(〜/图片/ ico_tablesortoffset_asc.gif),ALT =ASC箭/>}
            @if(ViewBag.currentOrder ==降序&放大器;&安培; ViewBag.CurrentField ==VP)
            {< IMG ID =降序SRC =@ Url.Content(〜/图片/ ico_tablesortoffset_desc.gif),ALT =说明箭/>}
        < /第i
        <第i个
            @ Html.ActionLink(导演/ SR MGR。,,新的{中将sortOrder = ViewBag.currentOrder ==ASC DESC?:ASC=的SortField导演,VERS = ViewBag.SelectedVersion})
            @if(ViewBag.currentOrder ==ASC&放大器;&安培; ViewBag.CurrentField ==董事)
            {< IMG ID =ASCSRC =@ Url.Content(〜/图片/ ico_tablesortoffset_asc.gif),ALT =ASC箭/>}
            @if(ViewBag.currentOrder ==降序&放大器;&安培; ViewBag.CurrentField ==董事)
            {< IMG ID =降序SRC =@ Url.Content(〜/图片/ ico_tablesortoffset_desc.gif),ALT =说明箭/>}
        < /第i
        百分位>< /第i
    < / TR>
@ {
    变种row_class =奇;
}@foreach(以型号VAR项)
{
    row_class = row_class ==奇? 连:奇;
    < TR类=@ row_class>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.PA)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.MAJOR_PROGRAM)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.INVESTMENT_AREA)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.VERSION)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.VP)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.DIRECTOR)
        < / TD>
        &所述; TD>
            @ Html.ActionLink(编辑,编辑,新{ID = item.IAMP_PK})|            @ Html.ActionLink(删除,删除,新{ID = item.IAMP_PK})
        < / TD>
    < / TR>
}< /表>< D​​IV CLASS =横条>
    < D​​IV CLASS =左撇子>
    页@(Model.PageCount< Model.PageNumber 0:Model.PageNumber)
    动@ Model.PageCount
    &安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;
    @if(Model.Has previousPage)
    {
        @ Html.ActionLink(<<,,新页面{= 1,中将sortOrder = ViewBag.CurrentSort,currentFilter = ViewBag.CurrentFilter,VERS = ViewBag.SelectedVersion})
        @ Html.Raw();
        @ Html.ActionLink(< preV,,新的{页= Model.PageNumber - 1,中将sortOrder = ViewBag.CurrentSort,currentFilter = ViewBag.CurrentFilter,VERS = ViewBag.SelectedVersion})
    }
    其他
    {
        @:其中;&下;
        @ Html.Raw();
        @:或其可preV
    }    @if(Model.HasNextPage)
    {
        @ Html.ActionLink(下一步>中,新{页= Model.PageNumber + 1 =中将sortOrder ViewBag.CurrentSort,currentFilter = ViewBag.CurrentFilter,VERS = ViewBag.SelectedVersion})
        @ Html.Raw();
        @ Html.ActionLink(>>中,新{页= Model.PageCount,中将sortOrder = ViewBag.CurrentSort,currentFilter = ViewBag.CurrentFilter,VERS = ViewBag.SelectedVersion})
    }
    其他
    {
        @:下一个>
        @ Html.Raw()
        @:>>
    }
    < / DIV>
    < D​​IV CLASS =右倾>
    从@ Model.TotalItemCount记录显示记录@ Model.FirstItemOnPage为@ Model.LastItemOnPage
    < / DIV>
< / DIV>
< BR />< BR />
< D​​IV CLASS =右倾>
@ Html.ActionLink(下载以Excel格式,PaExcelOutput,霸,新的{VERS = ViewBag.SelectedVersion,currentFilter = ViewBag.currentFilter,中将sortOrder = ViewBag.CurrentSort,@class =Button1的})< / DIV>


解决方案

好吧,我知道,我只是张贴了这个问题,一个小时前,但我想通了,并希望其他人会发现这个答案有帮助,如果他们有同样的问题。我的excel调用的问题是,是它有许多变数。

行动链接的下载按钮,看起来像在此之前

 < D​​IV CLASS =右倾>
@ Html.ActionLink(下载以Excel格式,PaExcelOutput,霸,新的{VERS = ViewBag.SelectedVersion,currentFilter = ViewBag.currentFilter,中将sortOrder = ViewBag.CurrentSort,@class =Button1的})< / DIV>

我改成了这个

 < D​​IV CLASS =右倾>@ Html.ActionLink(下载以Excel格式,PaExcelOutput,新的{VERS = ViewBag.SelectedVersion,currentFilter = ViewBag.currentFilter,中将sortOrder = ViewBag.CurrentSort},{新@class =Button1的})< / DIV>

通过删除从我删除了额外的变量的句子是导致viewbags不变量传递给Excel的输出。

ActionLink的的方法应该如下所示

  ActionLink的(的HtmlHelper,字符串,字符串,对象,对象)

现在我知道了......,知道是成功的一半!

I am trying to filter the rows that are sent to a excel file. I have been able to figure out how to export row data to excel, but now I need to send filtered data to an excel sheet since I added a version dropdown. What happens is I select the version I want to see from the dropdown.

Ex. the version is 12.1.

The view will only bring back data that has a version number 12.1 associated with it. Now when I want to download the filtered rows in an excel file. I press the download to excel file button and it open the file in a excel file except it brings all the rows instead of the filtered rows. I thought that all I had to do was add the appropriate viewbag and viewdata variables to the call and it would work, no such luck, it still keeps bringing every row. Could you guys take a look at my code and help me figure out what I am doing wrong? I have posted the code below please let me know if you need more information for better diagnosis, I will be checking back often! Thanks for your help!

Excel Controller

[Authorize]
        public ActionResult PaExcelOutput(string sortField, string currentFilter, string searchString, int? page, string VERS)
        {

            int pageNumber = (page ?? 1); // start page number

            var PaExcel = from P in db.iamp_mapping
                          select P;


            ViewBag.CurrentField = sortField = String.IsNullOrEmpty(sortField) ? "IAMP_PK" : sortField; // Provides the field to sort
            ViewBag.CurrentFilter = searchString; // Provides the view with the current filter string

            ViewData["SelectedVersion"] = VERS;
            ViewData["currentFilter"] = currentFilter;
            ViewData["VERS"] = new SelectList(PaExcel.Select(x => x.VERSION).Distinct());

            if (!String.IsNullOrEmpty(VERS))
            {
                PaExcel = PaExcel.Where(p => p.VERSION.ToUpper().Contains(VERS));
            }

            if (Request.HttpMethod == "GET")
            {
                searchString = currentFilter; //sets the currentFilter equal to Searchstring
            }

            else
            {
                page = 1;                   // defaults to page 1
            }




            return View(PaExcel);
        }

Excel Output View

@model IEnumerable<DBFirstMVC.Models.iamp_mapping>

<!--This is what the MVC uses to output the correct rows to Excel -->

@{
    Layout = null;
    Response.AddHeader("Content-Type", "application/vnd.ms-excel");  
}




<table>
    <tr>
        <th>
            PA
        </th>
        <th>
            VERSION
        </th>
        <th>
            INVESTMENT AREA
        </th>
        <th>
            MAJOR PROGRAM
        </th>
        <th>
            PA SUB PROGRAM
        </th>
        <th>
            VP
        </th>
        <th>
            DIRECTOR
        </th>
        <th>
            SPA
        </th>
        <th>
            PA INITIATIVE
        </th>
        <th>
            NOTES
        </th>
        <th>
            TEAM BC
        </th>
        <th>
            PA CC
        </th>
        <th>
            PA DESCRIPTION
        </th>
        <th>
            PA MANAGER
        </th>
        <th>
            SPA CC
        </th>
        <th>
            TIER 1
        </th>
        <th>
            TIER 2
        </th>
        <th>
            TIER 3
        </th>
        <th>
            TIER 4
        </th>
        <th>
            CFP VIEW NV/NONNV
        </th>
        <th>
            CFP VIEW
        </th>
        <th>
            CREATION DATE
        </th>
        <th>
            CAF PROJECT NAME
        </th>
        <th>
            CAF IA
        </th>
        <th>
            CAF MP
        </th>
        <th>
            CAF LEAD DIR
        </th>
        <th>
            CAF LEAD MGR
        </th>
        <th>
            CAF CC DIR
        </th>
        <th>
            CAF CC MGR
        </th>
        <th>
            CAF CC
        </th>
        <th>
            CAF ID
        </th>
        <th>
            CREATED ON
        </th>
        <th>
            LAST EDITED
        </th>
        <th>
            PROJECT STATUS
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.PA)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.VERSION)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.INVESTMENT_AREA)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MAJOR_PROGRAM)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PA_SUB_PROGRAM)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.VP)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DIRECTOR)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.SPA)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PA_INITIATIVE)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NOTES)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TEAM_BC)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PA_CC)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PA_DESCRIPTION)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PA_MANAGER)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.SPA_CC)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TIER_1)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TIER_2)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TIER_3)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TIER_4)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CFP_VIEW_NV_NONNV)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CFP_VIEW)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CREATION_DATE)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CAF_PROJECT_NAME)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CAF_IA)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CAF_MP)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CAF_LEAD_DIR)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CAF_LEAD_MGR)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CAF_CC_DIR)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CAF_CC_MGR)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CAF_CC)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CAF_ID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CREATED_ON)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LAST_EDITED)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PROJECT_STATUS)
        </td>

    </tr>
}

</table>

Index View

<!--For stack overflow, This is where the actionlink that the download to excel button is located it is at the bottom of this code snippet called PAExcelOutput-->


@model PagedList.IPagedList<DBFirstMVC.Models.iamp_mapping>


@{
    ViewBag.Title = "PA Mapping";

}

@using PagedList;


<h2 class="corporate sifr">@ViewBag.Title</h2>


<div class="crossband">
@using (Html.BeginForm())
{

    <div class="lefty">        
        Filter by Version: @Html.DropDownList("VERS", null, "All", new { @class = "text" }) and by Criteria: @Html.TextBox("SearchString", "", new { @class = "text" })        
    </div>    
    <input type = "submit" class = "button1" value = "Go" />
}

<div class="righty">
    @Html.ActionLink("Add a new PA to the database", "Create", "Pa", null, new { @class = "button1" })

</div>
</div>

<div class="crossband">
    <div class="lefty">   
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
    of @Model.PageCount
    &nbsp;&nbsp;&nbsp;&nbsp;
    @if (Model.HasPreviousPage)
    {
        @Html.ActionLink("<<", "", new { page = 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, VERS = ViewBag.SelectedVersion })
        @Html.Raw(" ");
        @Html.ActionLink("< Prev", "", new { page = Model.PageNumber - 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, VERS = ViewBag.SelectedVersion })
    }
    else
    {
        @:<<
        @Html.Raw(" ");
        @:< Prev
    }

    @if (Model.HasNextPage)
    {
        @Html.ActionLink("Next >", "", new { page = Model.PageNumber + 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, VERS = ViewBag.SelectedVersion })
        @Html.Raw(" ");
        @Html.ActionLink(">>", "", new { page = Model.PageCount, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, VERS = ViewBag.SelectedVersion })
    }
    else
    {
        @:Next >
        @Html.Raw(" ")
        @:>>
    }
    </div>
    <div class="righty">
        Showing Records @Model.FirstItemOnPage to @Model.LastItemOnPage from @Model.TotalItemCount records
    </div>
</div>


<table>
    <tr>
        <th>
            @Html.ActionLink("PA", "", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "PA", VERS = ViewBag.SelectedVersion })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "PA")
            {<img id="asc" src="@Url.Content("~/Images/ico_tablesortoffset_asc.gif")" alt = "Asc Arrow" />}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "PA")
            {<img id="desc" src="@Url.Content("~/Images/ico_tablesortoffset_desc.gif")" alt = "Desc Arrow" />}
        </th>
        <th>        
            @Html.ActionLink("MAJOR PROGRAM", "", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "MAJOR_PROGRAM", VERS = ViewBag.SelectedVersion })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "MAJOR_PROGRAM")
            {<img id="asc" src="@Url.Content("~/Images/ico_tablesortoffset_asc.gif")" alt = "Asc Arrow" />}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "MAJOR_PROGRAM")
            {<img id="desc" src="@Url.Content("~/Images/ico_tablesortoffset_desc.gif")" alt = "Desc Arrow" />}           
        </th>
        <th>
            @Html.ActionLink("INVESTMENT AREA", "", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "INVESTMENT_AREA", VERS = ViewBag.SelectedVersion })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "INVESTMENT_AREA")
            {<img id="asc" src="@Url.Content("~/Images/ico_tablesortoffset_asc.gif")" alt = "Asc Arrow" />}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "INVESTMENT_AREA")
            {<img id="desc" src="@Url.Content("~/Images/ico_tablesortoffset_desc.gif")" alt = "Desc Arrow" />}   
        </th>
        <th>
            @Html.ActionLink("Version", "", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "VERSION", VERS = ViewBag.SelectedVersion })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "VERSION")
            {<img id="asc" src="@Url.Content("~/Images/ico_tablesortoffset_asc.gif")" alt = "Asc Arrow" />}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "VERSION")
            {<img id="desc" src="@Url.Content("~/Images/ico_tablesortoffset_desc.gif")" alt = "Desc Arrow" />}   
        </th>
        <th>
            @Html.ActionLink("VP/SR. DIRECTOR", "", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "VP", VERS = ViewBag.SelectedVersion })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "VP")
            {<img id="asc" src="@Url.Content("~/Images/ico_tablesortoffset_asc.gif")" alt = "Asc Arrow" />}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "VP")
            {<img id="desc" src="@Url.Content("~/Images/ico_tablesortoffset_desc.gif")" alt = "Desc Arrow" />}   
        </th>
        <th>
            @Html.ActionLink("DIRECTOR/SR. MGR", "", new { sortOrder = ViewBag.currentOrder == "asc" ? "desc" : "asc", sortField = "DIRECTOR", VERS = ViewBag.SelectedVersion })
            @if (ViewBag.currentOrder == "asc" && ViewBag.CurrentField == "DIRECTOR")
            {<img id="asc" src="@Url.Content("~/Images/ico_tablesortoffset_asc.gif")" alt = "Asc Arrow" />}
            @if (ViewBag.currentOrder == "desc" && ViewBag.CurrentField == "DIRECTOR")
            {<img id="desc" src="@Url.Content("~/Images/ico_tablesortoffset_desc.gif")" alt = "Desc Arrow" />}   
        </th>       
        <th></th>
    </tr>
@{
    var row_class = "odd";
}

@foreach (var item in Model)
{
    row_class = row_class == "odd" ? "even" : "odd";    
    <tr class="@row_class">
        <td>
            @Html.DisplayFor(modelItem => item.PA)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MAJOR_PROGRAM)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.INVESTMENT_AREA)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.VERSION)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.VP)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DIRECTOR)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.IAMP_PK }) |

            @Html.ActionLink("Delete", "Delete", new { id = item.IAMP_PK })
        </td>
    </tr>
}

</table>

<div class="crossband">
    <div class="lefty">
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
    of @Model.PageCount
    &nbsp;&nbsp;&nbsp;&nbsp;
    @if (Model.HasPreviousPage)
    {
        @Html.ActionLink("<<", "", new { page = 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, VERS = ViewBag.SelectedVersion })
        @Html.Raw(" ");
        @Html.ActionLink("< Prev", "", new { page = Model.PageNumber - 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, VERS = ViewBag.SelectedVersion })
    }
    else
    {
        @:<<
        @Html.Raw(" ");
        @:< Prev
    }

    @if (Model.HasNextPage)
    {
        @Html.ActionLink("Next >", "", new { page = Model.PageNumber + 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, VERS = ViewBag.SelectedVersion })
        @Html.Raw(" ");
        @Html.ActionLink(">>", "", new { page = Model.PageCount, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, VERS = ViewBag.SelectedVersion })
    }
    else
    {
        @:Next >
        @Html.Raw(" ")
        @:>>
    }
    </div>
    <div class="righty">
    Showing Records @Model.FirstItemOnPage to @Model.LastItemOnPage from @Model.TotalItemCount records
    </div>
</div>
<br/><br/>
<div class="righty">
@Html.ActionLink("Download in Excel Format", "PaExcelOutput", "Pa", new { VERS = ViewBag.SelectedVersion, currentFilter = ViewBag.currentFilter, sortOrder = ViewBag.CurrentSort, @class = "button1" })

</div>

解决方案

Ok, I know I just posted this question an hour ago but I figured it out and hopefully others will find this answer helpful if they are having the same problem. The problem with my excel call was that was that it had to many variables.

The Action link for the download button looked like this before

<div class="righty">
@Html.ActionLink("Download in Excel Format", "PaExcelOutput", "Pa", new { VERS = ViewBag.SelectedVersion, currentFilter = ViewBag.currentFilter, sortOrder = ViewBag.CurrentSort, @class = "button1" })

</div>

I changed it to this

<div class="righty">

@Html.ActionLink("Download in Excel Format", "PaExcelOutput", new { VERS = ViewBag.SelectedVersion, currentFilter = ViewBag.currentFilter, sortOrder = ViewBag.CurrentSort }, new { @class = "button1" })

</div>

By removing the "Pa" from the sentence I removed the extra variable that was causing the viewbags to not pass the variable to the excel output.

The actionlink method should look like the following

ActionLink(HtmlHelper, String, String, Object, Object)

Now I know... and knowing is half the battle!

这篇关于使用MVC 3滤波器输出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆