How to create an input grid in ASP.NET C # - MVC5

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 emails like this:

< input name="email[ ]" />

In Controller I would get this email[] tag with all the emails added, already in ASP.NET I do not know what would be the best way to make a screen so I tried to do the same but in the controller the parameter (formColletion) of the Post does not arrive as an email array, it understands only as a single field. Note: I wanted to resolve without AJAX.

    
asked by anonymous 10.07.2015 / 21:09

2 answers

2

This is the wrong way to solve it. In ASP.NET MVC, you must either pass to the form or a collection of Models or a collection of ViewModels . For example in 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 populate 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 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 on how to do it.

    
10.07.2015 / 23:17
1

Well, a simple way to do this, in your view you put several 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]" />

There you have it in your controller:

public ActionResult NomeController(strin[] email)

That it will recover perfectly

    
11.07.2015 / 17:23