Search code examples
model-view-controllerrazorasp.net-core-mvcframeworks

How to use Lists in ASP NET Framework with the POST method?


I have the following Model in my ASP NET Framework project:

public class User
    {
        [Display(Name = "Email")]
        [DataType(DataType.EmailAddress)]
        [EmailAddress(ErrorMessage = "El formato del correo electrónico no es válido.")]
        [Required(ErrorMessage = "Debe de introducir un Correo Electrónico.")]
        public string Email { get; set; }
    }

And there is the controller methods:

public ActionResult AddUsers()
        {

            return View();
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        [AllowAnonymous]
        public async Task<ActionResult> AddUsers(List<User> users)
        {
            //Do something with users

            return View();
        }

In the form I want a List of the User Model that I mentioned above to be collected. When loading the view it is not known how many Users there will be. This will be indicated in a numeric type input and dynamically it will have to generate as many entries for Email as indicated in the numeric input. When the form is submitted I would like a List of Users to be submitted.

How can I dynamically generate inputs for the User model and have it saved in a list and then sent to the controller?

I expect my controller to receive a list of Users picked up from the form


Solution

  • From your description, I think when the page load, It will generate a input to show the count of list<User> and it will also generate as many input tag as the count of list<User>, So I create a simple demo to achieve it, Hope it can help you solve this issue.

    public IActionResult AddUsers()
            {
                //for testing, i hard code herer
                List<User> users = new List<User>()
                {
                    new User()
                    {
                        Email = "123456@gmail.com"
                    },
                    new User()
                    {
                        Email = "abcdef@gmail.com"
                    },
                    new User()
                    {
                        Email = "111111@gmail.com"
                    },
                    new User()
                    {
                        Email = "987654321@gmail.com"
                    },
                    new User()
                    {
                        Email = "555555@gmail.com"
                    }
                };
                return View(users);
            }
    

    View

    @model List<User>
    
    @using (Html.BeginForm("AddUsers", "Home", FormMethod.Post))
    {
        <input type="number" value="@Model.Count" disabled/>
        <br/>
    
        @for (int i = 0; i < Model.Count; i++)
        {
            <input name="User[@i].Email" value="@Model[i].Email"/>
            <br />
        }
    
        <input type="submit" value="Submit" />
    }
    

    When the view load, It will generate like:

    enter image description here

    Then when you submit the form, it can receive the data of list<user>

    enter image description here