Adding users to a list MVC

后端 未结 1 1804
北恋
北恋 2021-01-29 10:06

I have a form in which the user is supposed to enter Name and Wage and click a button Add. When the button \"Add\" is clicked, that user is supposed to

相关标签:
1条回答
  • 2021-01-29 10:50

    You're mostly on the right path excluding way you're trying to store your users list.

    Since ASP.NET MVC controller instance is created for every request and disposed after view is rendered and passed to the browser - it will be new controller holding new List<UserModel> created on every request.

    So you have to store it somewhere else (session variables, file on server's disk, database and so on). Usually database is best choice for this.

    In the case you want to store it in session variable, you should add something like this into Global.asax:

    protected void Session_Start(object sender, EventArgs e)
    {
        Session["myUsers"] = new List<UserModel>();
    }
    

    and then in your controller's methods you will be able to access this list as

    var list = Session["myUsers"] as List<UserModel>;
    
    0 讨论(0)
提交回复
热议问题