How do I Pass a Variable from one Controller to another using MVC Contrib?

548 views Asked by At

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

2

There are 2 answers

0
Lisa S. On BEST ANSWER

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"]);
1
Sunil Raj On

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.