raranibar
raranibar

Reputation: 1267

asp.net mvc4 RadioButtonList not select multiple radiobutton

I'm working with ASP.net MVC4 with razor and javascript In this image I can't select multiple radio, when I select an option the other option selected remains clean and not selected. enter image description here

and this is my code:

 @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>      

}

how I can select an option for each row?

this is my controller

[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");           
    }

Where ListaFrontaExt is this data (image) (this send for ViewBag to partialview _DatosInfoVehiculo.cshtml enter image description here

Upvotes: 0

Views: 7071

Answers (3)

John H
John H

Reputation: 14655

You need to use a for loop:

@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>
}

The problem is Html.RadioButtonFor is generating the same names for all of your radio buttons, so it's treating them as one single group.

Upvotes: 2

JC Lizard
JC Lizard

Reputation: 1056

Use different names for each set

        @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>
          }

more example: How to identify which radio button is accepted?

Upvotes: 0

Juan Rada
Juan Rada

Reputation: 3766

use 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>

Upvotes: 0

Related Questions