Nuno Santos

Nuno Santos

  • NA
  • 60
  • 350

Table with a DropDownListFor inside every row in a Partial View .NET

Mar 10 2021 2:57 PM

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:

  1. public ActionResult AlterarEstadoAfericao(GestaoAfericoesViewModel model)  
  2. {  
  3.     GestaoAfericoesDbo gestaoAfericoesDbo = new GestaoAfericoesDbo();  
  4.   
  5.     DbUtil dbUtil = new DbUtil();  
  6.     string connectionString = dbUtil.generateConnectionString(Connections.Endpoint);  
  7.   
  8.     IntranetDbContext db;  
  9.     db = new IntranetDbContext(connectionString);  
  10.   
  11.     var idEstado = db.AF_Estado.Where(a => a.descricao.Equals(model.SelectedEstadoRow)).ToList().First();  
  12.     int id_estado = Convert.ToInt32(idEstado.id);  
  13.   
  14.     try  
  15.     {  
  16.         var dbAF_afericao = db.AF_afericao.Find(model.idSelected);  
  17.         dbAF_afericao.id_estado_actual = Convert.ToInt32(id_estado);  
  18.         db.SaveChanges();  
  19.   
  20.     }  
  21.     catch(SqlException exc)  
  22.     {  
  23.         Console.WriteLine(exc);  
  24.     }  
  25.   
  26.     return RedirectToAction("/GestaoAfericoes");  
  27.   
  28. }  

Partial View:

  1. @using (Html.BeginForm("AlterarEstadoAfericao""Ferramentas", FormMethod.Post))  
  2. {  
  3.   
  4. <table id="table" class="table">  
  5.     <tr>  
  6.         <th>Id</th>  
  7.         <th>Descrição</th>  
  8.         <th>Início</th>  
  9.         <th>Fim</th>  
  10.         <th>Origem</th>  
  11.         <th>Estado</th>  
  12.         <th></th>  
  13.         <th></th>  
  14.     </tr>  
  15.     @if (Model.listGestaoAfericoes != null)  
  16.     {  
  17.         foreach (var item in Model.listGestaoAfericoes)  
  18.         {  
  19.               
  20.             <tr id="@item.id">  
  21.                 <td id="id">@item.id</td>  
  22.                 <td>@item.descricao</td>  
  23.                 <td>@item.data_ini_afericao</td>  
  24.                 <td>@item.data_fim_afericao</td>  
  25.                 <td id="origem">@item.origem_afericao</td>  
  26.                 <td>@item.id_estado_actual</td>  
  27.   
  28.                 @Html.HiddenFor(model => model.idSelected, new { @Value = @item.id})  
  29.   
  30.                 <td>Estado: @Html.DropDownListFor(model => model.SelectedEstadoRow, (IEnumerable<SelectListItem>)Model.listEstados)</td>  
  31.                 <td>  
  32.   
  33.                     @Html.ActionLink("Alterar Estado""AlterarEstadoAfericao"null,  
  34.                        new { onclick = "return confirm('Tem a certeza que pretende alterar o estado?');", @class = "btn btn-info" })  
  35.   
  36.                 </td>  
  37.             </tr>  
  38.         }  
  39.     }  
  40.   
  41. </table>  
  42. }  

I tried another way to try a resolution to the problem, but no success.

Partial View:

  1. foreach (var item in Model.listGestaoAfericoes)  
  2. {  
  3.   
  4.     <tr id="@item.id">  
  5.         <td id="id">@item.id</td>  
  6.         <td>@item.descricao</td>  
  7.         <td>@item.data_ini_afericao</td>  
  8.         <td>@item.data_fim_afericao</td>  
  9.         <td id="origem">@item.origem_afericao</td>  
  10.         <td>@item.id_estado_actual</td>  
  11.   
  12.         @Html.HiddenFor(model => model.idSelected, new { @Value = @item.id })  
  13.   
  14.         <td>Estado: @Html.DropDownListFor(model => item.SelectedEstadoRow, (IEnumerable<SelectListItem>)Model.listEstados)</td>  
  15.   
  16.         <td>  
  17.             @Html.ActionLink("Alterar Estado""AlterarEstadoAfericao"new { item.id, item.SelectedEstadoRow },  
  18.                new { onclick = "return confirm('Tem a certeza que pretende alterar o estado?');", @class = "btn btn-info" })  
  19.         </td>  
  20.     </tr>  
  21. }  
  22.           

Controller:

  1. public ActionResult AlterarEstadoAfericao(Decimal id, string SelectedEstadoRow)  
  2. {  
  3.     GestaoAfericoesDbo gestaoAfericoesDbo = new GestaoAfericoesDbo();  
  4.   
  5.     DbUtil dbUtil = new DbUtil();  
  6.     string connectionString = dbUtil.generateConnectionString(Connections.Endpoint);  
  7.   
  8.     IntranetDbContext db;  
  9.     db = new IntranetDbContext(connectionString);  
  10.   
  11.     var idEstado = db.AF_Estado.Where(a => a.descricao.Equals(SelectedEstadoRow)).ToList().First();  
  12.     int id_estado = Convert.ToInt32(idEstado.id);  
  13.   
  14.     try  
  15.     {  
  16.         var dbAF_afericao = db.AF_afericao.Find(id);  
  17.         dbAF_afericao.id_estado_actual = Convert.ToInt32(id_estado);  
  18.         db.SaveChanges();  
  19.   
  20.     }  
  21.     catch (SqlException exc)  
  22.     {  
  23.         Console.WriteLine(exc);  
  24.     }  
  25.   
  26.     return RedirectToAction("/GestaoAfericoes");  
  27.   
  28. }  

The id came to controller with correct value, but SelectedEstadoRow have a null value on the controller.


Answers (1)