0
I’m sent to my model typed for a view and loaded the data into a table where I enclose a checkbox
for each line. The user will select the items that matter to be later saved in the database. My doubt is precisely at this point. How do I send to controller the items. In addition to my data list I also send other information.
My model that’s the one:
public int Id { get; set; }
public string CodigoUsuario { get; set; }
public string NomeUsuario { get; set; }
public List<Fornecedor> ListaFornecedorViewModels { get; set; }
Man controller that’s the one:
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’ll need to give two post
in the same action "New user"... The first to search for the list of suppliers and the second to send the data plus the selected list items. How am I gonna do that?
EDIT
I made the adjustments @Gypsy mentioned in the model and I’m using Begincollectionitem, but the selected one is always coming as false and the other fields as null. See the images:
What am I doing wrong?
Has already installed the Begincollectionitem?
– Leonel Sanches da Silva
And what that could help me Gypsy?
– Luciano Carlos
Solving your problem. If not, I put an answer.
– Leonel Sanches da Silva
I don’t know how to use what you mentioned. Please, I await your reply.
– Luciano Carlos
If you want the fields to go to the Controller, you need to turn them into
@Html.EditorFor()
or@Html.HiddenFor()
, but I only recommend doing this for the Supplier ID. Not for all fields.– Leonel Sanches da Silva
Good... I will do the hiddenfor() to get the ID. However my doubt is still when I send the data to the controller and the check are all null.
– Luciano Carlos
Worked now?
– Leonel Sanches da Silva