如何解决局部视图 .NET MVC 中的每一行内都有一个 DropDownListFor 的表
我在局部视图中遇到一个表格问题,其中每一行都有一个用于状态列表的 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 在控制器上有一个空值。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。