问题描述
我在局部视图中遇到一个表格问题,其中每一行都有一个用于状态列表的 dropDownListFor 和一个“更改状态”按钮。
但我的问题是,如果我有 3 行并在视图模型到达控制器时更改状态,则所选状态是第一行的状态,而不是所选行上更改的状态。
控制器:
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");
}
部分视图:
@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>
}
谁能帮我解决这个问题?
问候
解决方法
我尝试了另一种方法来尝试解决问题,但没有成功。
部分视图:
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>
}
控制器:
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");
}
id 以正确的值到达控制器,但 SelectedEstadoRow 在控制器上有一个空值。