开发者

Saving a list of items in Application State ASP.NET MVC?

开发者 https://www.devze.com 2022-12-29 14:16 出处:网络
I\'m having a bit of trouble with knowing how I can save items from a list I have in Application state in the global.asax as- Application[\"\"].

I'm having a bit of trouble with knowing how I can save items from a list I have in Application state in the global.asax as- Application[""].

My controller basically takes in some user input, I then pass this to another classes Method as parameters it gets added to a list all the time. This data thats getting added to the list I want to store it, but without using a db. By using Application State. . I have been instantiating this class and calling its method to return the list of items but I dont think Application State is saving it.

Here is what I have so far. .

        protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);


        TimeLineInformation t = new TimeLineInformation();
        IList<SWTimeEnvInfoDTO> g = t.getInfo();

        Application["AppID"] =  g;
    }

^ Global.asax

        IList<SWTimeEnvInfoDTO> result = new List<SWTimeEnvInfoDTO>();

    public  void returnTimeLineInfo(string SWrelease, string EnvName, DateTime SDate, DateTime EDate) {


        SWTimeEnvInfoDTO myDTO = new SWTimeEnvInfoDTO();
        myDTO.softwareReleaseName = SWrelease;
        myD开发者_如何学PythonTO.environmentName = EnvName; 
        myDTO.StartDate = SDate;
        myDTO.EndDate = EDate;

        result.Add(myDTO);

        getInfo();


    }


    public IList<SWTimeEnvInfoDTO> getInfo() {

        return result;

    }

^ class im calling

The SWTimeEnvInfoDTO type has get and set methods for the data.

I am calling the application from a View as well. It works with a string

Application["AppID"] = "fgt";

and shows this once i read it from my view.


Updated: You should use casting to retrieve variable you have saved at Application, and save it if it has changed:

IList<SWTimeEnvInfoDTO> result = Application["AppID"] != null ? (IList<SWTimeEnvInfoDTO>)Application["AppID"] : new List<SWTimeEnvInfoDTO>(); // retrieve data  

result.Add(...);               // add some new items
Application["AppID"] = result; // save added values
0

精彩评论

暂无评论...
验证码 换一张
取 消