I'm facing a problem with a table inside a partial view, where each row have an dropDownListFor for status list and a button "change status".
But my problem is, if i have 3 row's and change the status when the view model get to controller the selected satus is the status of first row and not the status changed on selected row.
Controller:
- public ActionResult AlterarEstadoAfericao(GestaoAfericoesViewModel model)
- {
- GestaoAfericoesDbo gestaoAfericoesDbo = new GestaoAfericoesDbo();
-
- DbUtil dbUtil = new DbUtil();
- string connectionString = dbUtil.generateConnectionString(Connections.Endpoint);
-
- IntranetDbContext db;
- db = new IntranetDbContext(connectionString);
-
- var idEstado = db.AF_Estado.Where(a => a.descricao.Equals(model.SelectedEstadoRow)).ToList().First();
- int id_estado = Convert.ToInt32(idEstado.id);
-
- try
- {
- var dbAF_afericao = db.AF_afericao.Find(model.idSelected);
- dbAF_afericao.id_estado_actual = Convert.ToInt32(id_estado);
- db.SaveChanges();
-
- }
- catch(SqlException exc)
- {
- Console.WriteLine(exc);
- }
-
- return RedirectToAction("/GestaoAfericoes");
-
- }
Partial View:
- @using (Html.BeginForm("AlterarEstadoAfericao", "Ferramentas", FormMethod.Post))
- {
-
- <table id="table" class="table">
- <tr>
- <th>Id</th>
- <th>Descrição</th>
- <th>Início</th>
- <th>Fim</th>
- <th>Origem</th>
- <th>Estado</th>
- <th></th>
- <th></th>
- </tr>
- @if (Model.listGestaoAfericoes != null)
- {
- foreach (var item in Model.listGestaoAfericoes)
- {
-
- <tr id="@item.id">
- <td id="id">@item.id</td>
- <td>@item.descricao</td>
- <td>@item.data_ini_afericao</td>
- <td>@item.data_fim_afericao</td>
- <td id="origem">@item.origem_afericao</td>
- <td>@item.id_estado_actual</td>
-
- @Html.HiddenFor(model => model.idSelected, new { @Value = @item.id})
-
- <td>Estado: @Html.DropDownListFor(model => model.SelectedEstadoRow, (IEnumerable<SelectListItem>)Model.listEstados)</td>
- <td>
-
- @Html.ActionLink("Alterar Estado", "AlterarEstadoAfericao", null,
- new { onclick = "return confirm('Tem a certeza que pretende alterar o estado?');", @class = "btn btn-info" })
-
- </td>
- </tr>
- }
- }
-
- </table>
- }
I tried another way to try a resolution to the problem, but no success.
Partial View:
- foreach (var item in Model.listGestaoAfericoes)
- {
-
- <tr id="@item.id">
- <td id="id">@item.id</td>
- <td>@item.descricao</td>
- <td>@item.data_ini_afericao</td>
- <td>@item.data_fim_afericao</td>
- <td id="origem">@item.origem_afericao</td>
- <td>@item.id_estado_actual</td>
-
- @Html.HiddenFor(model => model.idSelected, new { @Value = @item.id })
-
- <td>Estado: @Html.DropDownListFor(model => item.SelectedEstadoRow, (IEnumerable<SelectListItem>)Model.listEstados)</td>
-
- <td>
- @Html.ActionLink("Alterar Estado", "AlterarEstadoAfericao", new { item.id, item.SelectedEstadoRow },
- new { onclick = "return confirm('Tem a certeza que pretende alterar o estado?');", @class = "btn btn-info" })
- </td>
- </tr>
- }
-
Controller:
- public ActionResult AlterarEstadoAfericao(Decimal id, string SelectedEstadoRow)
- {
- GestaoAfericoesDbo gestaoAfericoesDbo = new GestaoAfericoesDbo();
-
- DbUtil dbUtil = new DbUtil();
- string connectionString = dbUtil.generateConnectionString(Connections.Endpoint);
-
- IntranetDbContext db;
- db = new IntranetDbContext(connectionString);
-
- var idEstado = db.AF_Estado.Where(a => a.descricao.Equals(SelectedEstadoRow)).ToList().First();
- int id_estado = Convert.ToInt32(idEstado.id);
-
- try
- {
- var dbAF_afericao = db.AF_afericao.Find(id);
- dbAF_afericao.id_estado_actual = Convert.ToInt32(id_estado);
- db.SaveChanges();
-
- }
- catch (SqlException exc)
- {
- Console.WriteLine(exc);
- }
-
- return RedirectToAction("/GestaoAfericoes");
-
- }
The id came to controller with correct value, but SelectedEstadoRow have a null value on the controller.