How to create an input grid in Asp.net C# - MVC5

Asked

Viewed 313 times

2

Basically I have a form and I am opening a window (modal) with a list of employees and by javascript I can add the email of the same in the form that will be saved, in another language in this case I would solve creating the input of the emails so:

< input name="email[ ]" />

At Controller I would get this tag email[] with all the emails added, already in Asp.net do not know what would be the best way to make a screen like this, I tried to do the same but in the controller the parameter(formColletion) Post does not arrive as an e-mail array, it understands only as a single field. Note: I wanted to solve without AJAX.

2 answers

2


This is the wrong way to solve it. In ASP.NET MVC, you must pass to the form or a collection of Models or a collection of Viewmodels. For example of Viewmodel, you must declare a class as follows:

public class FuncionarioViewModel 
{
    public int FuncionarioId { get; set; }
    [EmailAddress]
    public String Email { get; set; }
}

When assembling your form, you should use the Nuget Begincollectionitem package. It prepares your form to correctly fill in the object that will go to the Controller. This can be done by declaring a master class for the form and using the Viewmodel already defined for the details:

public class MeuFormularioViewModel
{
    ...
    public ICollection<FuncionarioViewModel> Funcionarios { get; set; }
    ...
}

Your Action in the Controller will look like this:

public ActionResult Salvar(MeuFormularioViewModel viewModel) 
{
    // Coloque sua lógica para salvar os registros aqui
}

I answered several very similar questions, which you can check here. I recommend reading for details of how to do.

1

Well, a simple way to do this, in your view you put various inputs with the same name.

<input name="email" value="[email protected]" />
<input name="email" value="[email protected]" />
<input name="email" value="[email protected]" />
<input name="email" value="[email protected]" />

Ai on your controller just take this:

public ActionResult NomeController(strin[] email)

That he will recover perfectly

Browser other questions tagged

You are not signed in. Login or sign up in order to post.