2

I am new in MVC.
I have a class in Models like below.

public class UserInfo
{
  public string Name{get;set;}
  public string Surname{get;set;}
}

And I have a controller like this.

public class HomeController : Controller
    {
    //
    // GET: /Home/

    List<UserInfo> Users = new List<UserInfo>(); 
    public ActionResult Index()
    {
        return View();
    }

   public ActionResult Create()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Create(UserInfo userInfo)
    {
        if (ModelState.IsValid)
        {
            Users.Add(userInfo);
            return RedirectToAction("Index");
        }
        return View();
    }

}

And every time when i press Create button in browser, it redirects to Index View. But List<UserInfo> is empty. Why it happens? How to save data into List and show from grid or table from that List?

1
  • 2
    It's because you're not persisting the model and not passing it around. Commented Jan 22, 2014 at 19:35

5 Answers 5

3

Controllers are stateless. A new instance is created each time you call an action, this is why your list is always empty.

If you want to persist the list between controller actions, you will need some sort of persistence mechanism - database, caching, etc.

Sign up to request clarification or add additional context in comments.

Comments

1

Make your list static

static List<UserInfo> Users = new List<UserInfo>(); 

Comments

1

I believe Maess pointed out the details first.

Even though your [HTTPPost] Create action adds the new user to the List object, there does not appear to be any means to save the entered data to persistent storage. Every time you instantiate the controller, the List object will be empty, as you have currently coded it.

If you want the data stored to a database, check out this tutorial from asp.net for one method:

http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/accessing-your-models-data-from-a-controller

Comments

1

In your view put this reference for object:

Inherits="System.Web.Mvc.ViewPage<List<UserInfo>>" %>

And in your class change this:

List<UserInfo> Users = new List<UserInfo>();
    public ActionResult Index()
    {
        return View(Users);
    }

but for you to save the list data in memory, you'd better declare the List object as static, because then he retains the information already added. For example:

 public class HomeController : Controller
    {
        public static List<UserInfo> Users = null; 

        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View(Users);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(UserInfo userInfo)
        {
            if (ModelState.IsValid)
            {
                Users.Add(userInfo);
                return RedirectToAction("Index");
            }
            return View();
        }

    }

2 Comments

sir here how can I check if (Model.Users.Count() != null) inside view html page?
@AbhiJA Hi, just check the Model like this if(Model.Count >0) I'm supposing that you're using the list as Model.
1

Everytime you create a List<T> with new keyword it's instance is created which doesn't have any kind of data, so get data from database using your DatabaseContext and bind it to your list like this:

List<UserInfo> Users = new List<UserInfo>();
Users.Add(_databasecontext.Users.ToList());

and return it to the view.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.