如何使用 MVC Contrib 将变量从一个控制器传递到另一个控制器?

发布于 2024-12-28 05:27:56 字数 387 浏览 1 评论 0原文

Jeremy Skinner 有一个关于从 MVC Contrib 导出电子表格的博客和视频。问题是他的视频或博客没有进行任何过滤。在与包含网格的页面关联的控制器中,我有过滤器,在与“导出到电子表格”关联的控制器中,我需要该过滤器而不需要重置。问题是,每次我单击“导出到”时,变量都会重置电子表格”链接。如何在不重置的情况下将该变量从一个控制器获取到另一个控制器?

这是 Jeremy 的链接,http://www.jeremyskinner.co.uk/2010/04/28 /mvccontrib-grid-presentation 谢谢!

Jeremy Skinner has a blog and a video about exporting a spreadsheet from MVC Contrib. The problem is there is no filtering done in his video or blog. In the controller associated with page containing the grid, I have the filters and in the controller associated with the "Export to Spreadsheet, I need that filter without being reset. The problem is, the variable gets reset everytime I click on the "Export to Spreadsheet" link. How do I get that variable from one controller to another without resetting?

Here is Jeremy's link, http://www.jeremyskinner.co.uk/2010/04/28/mvccontrib-grid-presentation. Thank you!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冧九 2025-01-04 05:27:56

我最终创建了一个像这样的会话变量:

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"]);

I ended up creating a Session variable like this:

1- Enable the session variable by editing the web.config with this:.

<configuration>
   <system.web>
      <sessionState cookieless="true" regenerateExpiredSessionId="true" />
   </system.web>
</configuration>

2- Create Session State in first controller

Session["FirstName"] = FirstNameTextBox.Text;

3- Use Session State in second contoller

string firstName = (string)(Session["FirstName"]);
旧瑾黎汐 2025-01-04 05:27:56

使用 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”按钮单击中调用导出操作。

Use TempData[""] object.

Your ViewModel should look somewhat like:

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

Your Controller Action:

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

Export Function:

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

Call the export action in your "Export to Excel" button click.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文