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?