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" class="table"> -
-
Id -
Descrição -
Início -
Fim -
Origem -
Estado -
-
-
- @if (Model.listGestaoAfericoes != null)
- {
- foreach (var item in Model.listGestaoAfericoes)
- {
-
-
"@item.id"> -
"id">@item.id -
@item.descricao -
@item.data_ini_afericao -
@item.data_fim_afericao -
"origem">@item.origem_afericao -
@item.id_estado_actual -
- @Html.HiddenFor(model => model.idSelected, new { @Value = @item.id})
-
-
Estado: @Html.DropDownListFor(model => model.SelectedEstadoRow, (IEnumerable)Model.listEstados) -
-
- @Html.ActionLink("Alterar Estado", "AlterarEstadoAfericao", null,
- new { onclick = "return confirm('Tem a certeza que pretende alterar o estado?');", @class = "btn btn-info" })
-
-
-
- }
- }
-
- }
I tried another way to try a resolution to the problem, but no success.
Partial View:
- foreach (var item in Model.listGestaoAfericoes)
- {
-
"@item.id"> "id">@item.id @item.descricao @item.data_ini_afericao @item.data_fim_afericao "origem">@item.origem_afericao @item.id_estado_actual - @Html.HiddenFor(model => model.idSelected, new { @Value = @item.id })
Estado: @Html.DropDownListFor(model => item.SelectedEstadoRow, (IEnumerable )Model.listEstados) - @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" })
- }
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.
Kiran MohantyPosted Mar 11, 2021, 7:22 AM