Jeremy Skinner有一个关于从MVC Contrib导出电子表格的博客和视频。问题是他的视频或博客中没有进行过滤。在与包含网格的页面相关联的控制器中,我有过滤器,并且在与“导出到电子表格”相关联的控制器中,我需要过滤而不重置。问题是,每次我点击“导出到”时变量都会重置电子表格“链接。如何在不重置的情况下将该变量从一个控制器转换到另一个控制器?
这是Jeremy的链接http://www.jeremyskinner.co.uk/2010/04/28/mvccontrib-grid-presentation。谢谢!!
答案 0 :(得分:0)
我最终创建了一个这样的Session变量:
1-通过编辑web.config启用会话变量:。
<configuration>
<system.web>
<sessionState cookieless="true" regenerateExpiredSessionId="true" />
</system.web>
</configuration>
2-在第一个控制器中创建会话状态
Session["FirstName"] = FirstNameTextBox.Text;
3-在第二个控制器中使用会话状态
string firstName = (string)(Session["FirstName"]);
答案 1 :(得分:0)
使用TempData [“”]对象。
您的ViewModel应该看起来像:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using MvcContrib.Pagination;
using MvcContrib.UI.Grid;
using System.Web.Mvc;
namespace MyMVCProject.ViewModels
{
public class SubscriptionViewModel
{
public int SubscriptionID { get; set; }
public string SubNo { get; set; }
}
public class SubscriptionListContainerViewModel
{
public IPagination<SubscriptionViewModel> SubscriptionPagedList { get; set; }
public SubscriptionFilterViewModel Filters { get; set; }
public GridSortOptions GridSortOptions { get; set; }
public int? TotalCount { get; set; }
}
public class SubscriptionFilterViewModel
{
public int? CustomerID { get; set; }
public int? PlanID { get; set; }
}
}
您的控制器操作:
public ActionResult Index(SubscriptionListContainerViewModel model, GridSortOptions gridSortOptions, int? page)
{
SubscriptionFilterViewModel filter = new SubscriptionFilterViewModel();
if (model.Filters != null)
{
filter.CustomerID = model.Filters.CustomerID;
filter.PlanID = model.Filters.PlanID;
}
TempData["Filters"]=filter;
//code for IPagination<SubscriptionViewModel> population.
}
导出功能:
public void Export()
{
SubscriptionFilterViewModel filter = (SubscriptionFilterViewModel)TempData["Filters"];
TempData["Filters"]=filter;
//code for IPagination<SubscriptionViewModel> population and excel creation.
//output the excel after creation
Guid fileId = Guid.NewGuid();
string strFileName = Convert.ToString(fileId) + ".xls";
string strFilePathnName = HttpContext.Server.MapPath ("~/Content/Uploads/Excels/Export/") + strFileName;
MemoryStream file = new MemoryStream();
hssfworkbook.Write(file);
System.IO.File.WriteAllBytes(strFilePathnName, file.GetBuffer());
System.IO.FileInfo inf = new FileInfo(strFilePathnName);
HttpContext.Response.AddHeader("Content-Disposition", "attachment; filename=Blogs" + inf.Extension);
HttpContext.Response.ContentType = "application/ms-excel";
HttpContext.Response.TransmitFile(HttpContext.Server.MapPath ("~/Content/Uploads/Excels/Export/" + strFileName));
}
在“导出到Excel”按钮中调用导出操作单击。