B
B
birinci20122014-01-22 21:26:51
ASP.NET
birinci2012, 2014-01-22 21:26:51

How to store information in a Sheet in ASP.NET MVC 4.0?

Hello.
I am new to MVC.
I have such a class.

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

And I have such a controller.
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 I click the Create button in the browser, the List Users is empty.
Why is this happening? What is the problem? How to store data in a sheet?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
D
Dmitry Guketlev, 2014-01-22
@birinci2012

Because you apparently have different instances of controllers. Make the Users field static eg.

N
Nikolai Turnaviotov, 2014-01-22
@foxmuldercp

The model class must also be public.
I advise, as a beginner, to go through two manuals on asp.net mvc 3/4 located on the official website - on one cd disk store and video
catalog www.asp.net/mvc/tutorials/mvc-4/getting-started-wi...
www.asp.net/mvc/tutorials/mvc-music-store/mvc-musi...
They will explain you well what and how, but, unfortunately, only the basics, then you have to climb into the jungle yourself.
, then you can, for example, contact me, I, in principle, started working with all this not so long ago and now I am writing my own version of home web accounting on asp.net mvc5, but I will try to help

V
Vadim Martynov, 2014-01-24
@Vadimyan

Hello. I agree with @foxmuldercp - the materials are helpful.
a static field in a controller is the worst possible solution. The MVC architecture assumes a model, which you ignore. The controller must work with a certain repository in terms of extracting and saving data. I will give a simplified example

public interface IUsersRepository
{
    UserInfo SaveUserInfo(UserInfo user);
    IReadOnlyCollection<UserInfo> GetUsers();
}

In this case, your controller may look like this:
(This is a test case, in practice you need to use DI through an IoC container to inject the repository)
public class HomeController : Controller
{
    private readonly IUsersRepository _usersRepository;
    public HomeController()
    {
        _usersRepository = new UsersRepository();
    }
        
    public ActionResult Index()
    {
        var users = _usersRepository.GetUsers();
        return View(users);
    }

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

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

At the same time, the work of saving users is transferred to the implementation of the IUsersRepository interface. Depending on your needs, you can implement storing a collection of users in a database, in a file on disk, in RAM (for this you need to either make the repository a singleton or code a static collection in it).
A few final notes:
1. Repositories are usually responsible for working with a specific data store at the save/delete/retrieve level, and there is no room for business logic. Therefore, there can be even more links - there is a business logic service for working with users, which retrieves data through the repository, and then forms a model to return to the controller. Thus from class EF code firstPerson model can be formed UserInfo.
2. Let me remind you again about @foxmuldercp materials , which can be very useful at the initial stages of learning mvc.

M
mrUlugbek, 2014-01-23
@mrUlugbek

yes MVC 5 they included Bootstrap

A
Alexey Gagarin, 2014-07-03
@Alexey_Gagarin

I didn’t understand, but who will transfer the model to the View, Uncle Vasya or what?
You are calling your index without a model. This is the first.
Second, as already mentioned: with each new request to the server, a new instance of the controller is created. Those. when a user is created, the first controller is created and the user is added to its list.
Calling Index creates another controller whose list is empty. So implement a storage for testing, like a static MemoryUserRepository class, and stick your leaf in there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question