checkbox + Model + Controller. How to pick the selected lines?

0

I'm sent my model to a view and loaded the data into a table where I include a checkbox for each line. The user will select the items of interest to be later recorded in the database. My question is right on this point. How do I send the items to the controller . In addition to my list of data I also send other information.

My model is this:

public int Id { get; set; }
public string CodigoUsuario { get; set; }
public string NomeUsuario { get; set; }
public List<Fornecedor> ListaFornecedorViewModels { get; set; }

My controller is this:

public ActionResult UsuarioNovo() {
    UsuarioViewModel usuarioViewModel = new UsuarioViewModel();
    ViewData["Colaborador"] = _userLogado;
    return View(usuarioViewModel);
}
[HttpPost]
public ActionResult UsuarioNovo(UsuarioViewModel usuarioViewModel)
{
    if (usuarioViewModel.ListaFornecedorViewModels != null)
    {
        if (!string.IsNullOrEmpty(usuarioViewModel.FornecedorBusca)) {
            int iCnpj;
            var listFornecedor = new BuscaDadosFornecedorBo().BuscaRequisicaoFornecedorBo(usuarioViewModel.FornecedorBusca, "");
            usuarioViewModel.ListaFornecedorViewModels = listFornecedor;
        }
    }
    else
    {
        //Gravar Dados
    }
    return View(usuarioViewModel);
}

And this is my View:

@using (@Html.BeginForm()) {
<div class="linha">
    <div class="campo item small">
        @Html.LabelFor(model => model.NomeUsuario)
        @Html.EditorFor(model => model.NomeUsuario)
    </div>
</div>

<div class="linha">
    <div class="campo item small">
        @Html.LabelFor(model => model.SobrenomeUsuario)
        @Html.EditorFor(model => model.SobrenomeUsuario)
    </div>
</div>

<div class="panel-content">
    <div class="linha">
        <div class="campo item">
            @Html.LabelFor(model => model.FornecedorBusca)
            @Html.EditorFor(model => model.FornecedorBusca)
        </div>
        <input class="btn btn-primary" type="submit" value="Buscar" />
    </div>
</div>  

@if (Model.ListaFornecedorViewModels != null) {
    <div class="table-custom">
        <table cellpadding="0" cellspacing="0" border="0" class="dataTable display">
            <thead>
                <tr>
                    <th class="sorting_disabled" rowspan="1" colspan="1" aria-label=" " style="width: 10px;"> </th>
                    <th>Selecionar</th>
                    <th>CNPJ</th>
                    <th>RazaoSocial</th>
                    <th>Municipio</th>                                    
                </tr>
            </thead>

            <tbody>
                @foreach (var forn in Model.ListaFornecedorViewModels) {
                    <tr>
                        <td></td>
                        <td><input type="checkbox" value="@forn.Codigocnpj" name="chkForn"/></td>
                        <td>@forn.Codigocnpj</td>
                        <td>@forn.RazaoSocial</td>
                        <td>@forn.Municipio</td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
}

}

I will need to give two post in the same action "NewUser" ... The first to search the vendor list and the second to send the data plus the selected list items. How will I do this?

EDIT

I made the settings that @ Gypsy mentioned in the model and I'm using BeginCollectionItem , however the selected is always coming as false and the other fields as null. See the pictures:

Andinmycodeitshowsthis:

What am I doing wrong?

    
asked by anonymous 01.10.2015 / 00:04

1 answer

2

It is not good practice to leave very different functions in the same Action , such as putting together a ViewModel and persists a new user. The correct would be you do the following:

public ActionResult UsuarioNovo() 
{
    UsuarioViewModel usuarioViewModel = new UsuarioViewModel {
        ListaFornecedorViewModels = new BuscaDadosFornecedorBo().BuscaRequisicaoFornecedorBo(usuarioViewModel.FornecedorBusca, "")
    };
    ViewData["Colaborador"] = _userLogado;
    return View(usuarioViewModel);
}

[HttpPost]
public ActionResult UsuarioNovo(UsuarioViewModel usuarioViewModel)
{
    if (usuarioViewModel.ListaFornecedorViewModels != null)
    {
        //Gravar Dados
    }

    return View(usuarioViewModel);
}

As I mentioned in commentary, this is a classic case of using the BeginCollectionItem package. There are lots of questions and answers you can rely on , but I'll leave the paved path to the solution.

If the goal is to work with a list of vendors with a CheckBox , it's only fair that you set the CheckBox to ViewModel , after all, you will not save the vendor. Just pick up if it was selected or not.

public class FornecedorViewModel
{
    public bool Selecionado { get; set; }
    public Fornecedor Fornecedor { get; set; }
}

Changing the ViewModel of User:

public class UsuarioViewModel
{
    public int Id { get; set; }
    public string CodigoUsuario { get; set; }
    public string NomeUsuario { get; set; }
    public List<FornecedorViewModel> Fornecedores { get; set; }
}

We'll have to change our method GET :

public ActionResult UsuarioNovo() 
{
    UsuarioViewModel usuarioViewModel = new UsuarioViewModel {
        Fornecedores = new BuscaDadosFornecedorBo()
                           .BuscaRequisicaoFornecedorBo(usuarioViewModel.FornecedorBusca, "")
                           .Select(f => new FornecedorViewModel { Fornecedor = f })
                           .ToList()
    };

    ViewData["Colaborador"] = _userLogado;
    return View(usuarioViewModel);
}

Your View , so it looks like this:

@if (Model.Fornecedores != null) {
    <div class="table-custom">
        <table cellpadding="0" cellspacing="0" border="0" class="dataTable display">
            <thead>
                <tr>
                    <th class="sorting_disabled" rowspan="1" colspan="1" aria-label=" " style="width: 10px;"> </th>
                    <th>Selecionar</th>
                    <th>CNPJ</th>
                    <th>RazaoSocial</th>
                    <th>Municipio</th>                                    
                </tr>
            </thead>

            <tbody>
                @foreach (var form in Model.Fornecedores) {
                    <tr>
                        <td></td>
                        <td>@Html.CheckBoxFor(_ => form.Selecionado)
                        <td>@form.Fornecedor.Codigocnpj</td>
                        <td>@form.Fornecedor.RazaoSocial</td>
                        <td>@form.Fornecedor.Municipio</td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
}

Now let's use BeginCollectionItem .

@if (Model.Fornecedores != null) {
    <div class="table-custom">
        <table cellpadding="0" cellspacing="0" border="0" class="dataTable display">
            <thead>
                <tr>
                    <th class="sorting_disabled" rowspan="1" colspan="1" aria-label=" " style="width: 10px;"> </th>
                    <th>Selecionar</th>
                    <th>CNPJ</th>
                    <th>RazaoSocial</th>
                    <th>Municipio</th>                                    
                </tr>
            </thead>

            <tbody>
                @foreach (var form in Model.Fornecedores) {
                    @using (Html.BeginCollectionItem("Fornecedores"))
                    {
                        <tr>
                            <td></td>
                            <td>@Html.CheckBoxFor(_ => form.Selecionado)
                            <td>@form.Fornecedor.Codigocnpj</td>
                            <td>@form.Fornecedor.RazaoSocial</td>
                            <td>@form.Fornecedor.Municipio</td>
                        </tr>
                    }
                }
            </tbody>
        </table>
    </div>
}

With this, it is very easy to finish the logic:

if (usuarioViewModel.Fornecedores != null)
{
    foreach (usuarioViewModel.Fornecedores.Where(f => f.Selecionado))
    {
        // Gravar dados
    }
}
    
01.10.2015 / 18:08