dilipkumar1007
dilipkumar1007

Reputation: 363

How to use variable defined in one actionresult into another actionresult?

public ActionResult crbtmis(string submitbuttonoperator, DateTime fromdate, DateTime todate, string operatorname)
{
    DateTime dfd = fromdate;
    DateTime dtd = todate;
    string soprtr = operatorname;
     if (Session["user"] != null && Session["user"].ToString() == "MISADMIN")
     {
         switch (submitbuttonoperator)
         {
             case "Export":
                 return (ExportOprtrlist( fromdate, todate, operatorname));
             case "Search":
                 return (SearchByOperator());
             default:
                 return (View("LogOn"));
         }
     }
     else
     {
         return RedirectToAction("LogOn");
     }
}

This is my actionresult which contains three variables: dfd,dtd and soprtr. How can I use these three variables in another actionresult ?

Upvotes: 0

Views: 1689

Answers (2)

Ben Pretorius
Ben Pretorius

Reputation: 4319

Your naming conventions is a bit weird and it is hard to figure out why exactly you want to do it in this manner but either way it can be done like so:

        public class MyClass
    {
        public DateTime dfd { get; set; }
        public DateTime dtd { get; set; }
        public string soprtr { get; set; }
    }

    public ActionResult crbtmis(string submitbuttonoperator, DateTime fromdate, DateTime todate, string operatorname)
    {
        MyClass myClass = new MyClass()
        {
            dfd = fromdate;
            dtd = todate;
            soprtr = operatorname;
        };

        Session["myClass"] = myClass;
        if (Session["user"] != null && Session["user"].ToString() == "MISADMIN")
        {
            switch (submitbuttonoperator)
            {
                case "Export":
                    return (ExportOprtrlist(fromdate, todate, operatorname));
                case "Search":
                    return (SearchByOperator());
                default:
                    return (View("LogOn"));
            }
        }
        else
        {
            return RedirectToAction("LogOn");
        }
    }

    public ActionResult LogOn()
    {
        if (Session["myClass"] != null)
        {
            MyClass myClass = (MyClass)Session["myClass"];
        }

        return View();
    }

Upvotes: 3

csteinmueller
csteinmueller

Reputation: 2487

The straight forward way is to save these variables into the Session object MSDN Controller.Session

Save:

Session["dfd"] = dfd;

retrieve:

DateTime dfd;
DateTime.TryParse(Session["dfd"].ToString(), out dfd);

Upvotes: 3

Related Questions