0

I'm trying to create a List of users in MVC, where there's a button to add users generated randomly and there's a delete button for each user to erase them from the list. I send it from the Controller to the View and it generates one user. When I try to add one more, it just changes it. I guess it deletes the items in the List. I'm trying to pass the list back to the Controller, but it doesn't work. Can someone help please?

My Model:

public class UsersClass
{
    public int Code { get; set; }
    public string Name { get; set; }

    public UsersClass(int Code, string Name)
    {
        this.Code = Code;
        this.Name = Name;
    }
}

My Controllers:

List<UsersClass> UsersList = new List<UsersClass>();
public ActionResult Index()
{
    return View(UsersList);
}

[HttpPost]
    public ActionResult AddUser(List<UsersClass> UsersList)
    {
        if (UsersList == null)
        {
            int a = 123;
            UsersList = new List<UsersClass>();
        }
        Random generator = new Random();

        string[] vez_nevek = new string[10] { "Kovács", "Szekeres", "Király", "Szabó", "Vicha", "Kozma", "Ferencz", "Pócsi", "Tinka", "Horváth" };
        string[] ker_nevek = new string[10] { "Lajos", "Barnabás", "Róbert", "Balázs", "János", "Béla", "Petra", "Anna", "Ferenc", "Attila" };

        string vezetek_nev = vez_nevek[generator.Next(vez_nevek.Length)];
        string kereszt_nev = ker_nevek[generator.Next(ker_nevek.Length)];

        UsersList.Add(new UsersClass(generator.Next(100000, 999999), vezetek_nev + " " + kereszt_nev)); 

        return View("~/Views/UserManagement/Index.cshtml", UsersList);
    }

And my View to add a user:

<h2>User Management</h2>

@using (Html.BeginForm("AddUser", "UserManagement", FormMethod.Post))
{
   int index = 0;
   foreach (var item in Model)
   {
       Html.Hidden("item[" + index + "].Code", item.Code);
       Html.TextBox("item[" + index + "].Name", item.Name);
       index++;
   }
   <input type="submit" value="Add User" />
}
  • Your attempting to generate `name` attributes that have no relationship to your model and would never bind to it (and the code you have shown will not even generate any html because of the missing `@` in `@Html.Hidden(...)` ). Generate your view correctly using `@for(int i = 0; i < Model.Count; i++) { @Html.HiddenForm(m => m[i].Code) @Html.TextBoxFor(m => m[i].Name) }`. Then refer [this answer](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) –  Oct 20 '16 at 20:41

2 Answers2

0

Your HttpPost method AddUser doesnt know about the list. You should create a ViewModel that contains a list of users. A ViewModel is one way of communicating data between View and Controller. The user edits the model while they are on the page and when they click save/delete the HttpPost method for that controller will be called. Create a parameter in the HttpPost method that is the model. The View knows about the ViewModel and will send it to the controller. see code below.

ViewModel:

public class UserManageViewModel
    {
        public List<UsersClass> users {get; set;}
    }

View: the line @model UserManageViewModel is crucial for your view to have or else it wont know what to send to the controller.

@model UserManageViewModel
<h2>User Management</h2>

@using (Html.BeginForm("AddUser", "UserManagement", FormMethod.Post))
{
   int index = 0;
   foreach (var item in Model.users)
   {
       Html.Hidden("item[" + index + "].Code", item.Code);
       Html.TextBox("item[" + index + "].Name", item.Name);
       index++;
   }
   <input type="submit" value="Add User" />
}

Controller:

public ActionResult Index()
{
    UserManageViewModel model = new UserManageViewModel();
    model.users = new List<UsersClass>();
    return View(model);
}

[HttpPost]
    public ActionResult AddUser(UserManageViewModel model)
    {
        if (model.users.IsEmpty() || model.users == null)
        {
            int a = 123;
            model.users = new List<UsersClass>();
        }
        Random generator = new Random();

        string[] vez_nevek = new string[10] { "Kovács", "Szekeres", "Király", "Szabó", "Vicha", "Kozma", "Ferencz", "Pócsi", "Tinka", "Horváth" };
        string[] ker_nevek = new string[10] { "Lajos", "Barnabás", "Róbert", "Balázs", "János", "Béla", "Petra", "Anna", "Ferenc", "Attila" };

        string vezetek_nev = vez_nevek[generator.Next(vez_nevek.Length)];
        string kereszt_nev = ker_nevek[generator.Next(ker_nevek.Length)];

        model.users.Add(new UsersClass(generator.Next(100000, 999999), vezetek_nev + " " + kereszt_nev)); 

        return View(model);
    }

Hope this helps. Cheers :)

JJohnson
  • 21
  • 3
-1

you should name all input the same same

@Html.TextBox("UsersList", item.Name);

- Action method

public ActionResult AddUser(List<string> UsersList){

}

  • Still returns to the Controller in a null List. @using (Html.BeginForm("AddUser", "UserManagement", FormMethod.Post)) { foreach (var item in Model) { @Html.Hidden("UsersList", item.Code); @Html.Hidden("UsersList", item.Name); } } – Mike Blázsovics Oct 20 '16 at 19:27
  • Still returns to the Controller in a null List. I used: @Html.Hidden("UsersList", item.Code); @Html.Hidden("UsersList", item.Name); – Mike Blázsovics Oct 20 '16 at 19:28