Sending List from View to Controller results in null List in MVC

时间:2016-10-20 12:35:38

标签: c# asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

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" />
}

2 个答案:

答案 0 :(得分: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 :)

答案 1 :(得分:-1)

你应该为所有输入命名相同的

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

- 行动方法

public ActionResult AddUser(List<string> UsersList){

}