我正在使用razor和javascript使用ASP.net MVC4
在此图像中,我无法选择多个收音机,当我选择一个选项时,所选的其他选项保持清洁而未选中。
这是我的代码:
@foreach (var item in ViewBag.DatosInfVehiFE){
<tbody>
<tr>
<td>@item.cprp_descripcion</td>
<td>@item.piv_cantidad</td>
<td>@item.cprp_idpartepre</td>
<td>
@Html.RadioButtonFor(m => m.parestadopieza, Crd.Web.Areas.Inspeccion.Models.v_PreInformacionVehi.Estado.Bueno, new { id = string.Format("B_{0}", item.cprp_idpartepre.ToString()) }) Bueno
@Html.RadioButtonFor(m => m.parestadopieza, Crd.Web.Areas.Inspeccion.Models.v_PreInformacionVehi.Estado.Regular, new { id = string.Format("R_{0}", item.cprp_idpartepre.ToString()) }) Regular
@Html.RadioButtonFor(m => m.parestadopieza, Crd.Web.Areas.Inspeccion.Models.v_PreInformacionVehi.Estado.Deficiente, new { id = string.Format("D_{0}", item.cprp_idpartepre.ToString()) }) Deficiente
</td>
<td>@item.piv_observacion</td>
}
我如何为每一行选择一个选项?
这是我的控制器
[HttpPost]
public ActionResult InsInformacionVehiculo(string IdInspeccion)
{
ViewBag.IdInspeccion = IdInspeccion;
List<Models.v_PreInformacionVehi> ListaInfVehi = InformacionVehiculo("INP0001", "", "").ToList();
List<Models.v_PreInformacionVehi> ListaFrontalExt = ListaInfVehi.Where(e => e.cprp_partipoiv == "E" && e.cprp_parsubtipoiv == "F").ToList();
ViewBag.DatosInfVehiFE = ListaFrontalExt;
return PartialView("~/Areas/Inspeccion/Views/raAcc/_DatosInfoVehiculo.cshtml");
}
ListaFrontaExt是这个数据(图像)的位置(这个为ViewBag发送到partialview _DatosInfoVehiculo.cshtml
答案 0 :(得分:2)
您需要使用for循环:
@for (var i = 0; i < ViewBag.DatosInfVehiFE.Count; i++)
{
<tbody>
<tr>
<td>@ViewBag.DatosInfVehiFE[i].cprp_descripcion</td>
<td>@ViewBag.DatosInfVehiFE[i].piv_cantidad</td>
<td>@ViewBag.DatosInfVehiFE[i].cprp_idpartepre</td>
<td>
@Html.RadioButton(String.Format("[{0}].parestadopieza", i), Crd.Web.Areas.Inspeccion.Models.v_PreInformacionVehi.Estado.Bueno, new { id = string.Format("B_{0}", ViewBag.DatosInfVehiFE[i].cprp_idpartepre.ToString()) }) Bueno
@Html.RadioButton(String.Format("[{0}].parestadopieza", i), Crd.Web.Areas.Inspeccion.Models.v_PreInformacionVehi.Estado.Regular, new { id = string.Format("R_{0}", ViewBag.DatosInfVehiFE[i].cprp_idpartepre.ToString()) }) Regular
@Html.RadioButton(String.Format("[{0}].parestadopieza", i), Crd.Web.Areas.Inspeccion.Models.v_PreInformacionVehi.Estado.Deficiente, new { id = string.Format("D_{0}", ViewBag.DatosInfVehiFE[i].cprp_idpartepre.ToString()) }) Deficiente
</td>
<td>@ViewBag.DatosInfVehiFE[i].piv_observacion</td>
}
问题是Html.RadioButtonFor
为所有单选按钮生成相同的名称,因此它将它们视为一个单独的组。
答案 1 :(得分:0)
使用RadioButton(http://msdn.microsoft.com/en-us/library/dd492690%28v=vs.108%29.aspx)
<ol class="Opt">
@foreach (var opt in quest.Options)
{
<li>
@Html.RadioButton("uniqueRadio", opt.Title)
@Html.Label(opt.Title)
</li>
}
</ol>
答案 2 :(得分:0)
为每组使用不同的名称
@foreach (var item in ViewBag.DatosInfVehiFE){
<tbody>
<tr>
<td>@item.cprp_descripcion</td>
<td>@item.piv_cantidad</td>
<td>@item.cprp_idpartepre</td>
<td>
@Html.RadioButtonFor(m => m.parestadopieza, Crd.Web.Areas.Inspeccion.Models.v_PreInformacionVehi.Estado.Bueno, new { id = string.Format("B_{0}", item.cprp_idpartepre.ToString()) , @name=item.name }) Bueno
@Html.RadioButtonFor(m => m.parestadopieza, Crd.Web.Areas.Inspeccion.Models.v_PreInformacionVehi.Estado.Regular, new { id = string.Format("R_{0}", item.cprp_idpartepre.ToString()), @name = item.name }) Regular
@Html.RadioButtonFor(m => m.parestadopieza, Crd.Web.Areas.Inspeccion.Models.v_PreInformacionVehi.Estado.Deficiente, new { id = string.Format("D_{0}", item.cprp_idpartepre.ToString()), @name = item.name }) Deficiente
</td>
<td>@item.piv_observacion</td>
</tr>
</tbody>
}