Question

I have a class for a PDF

public class UIClonePDFDetail
    {
        public int CatalogueID { get; set; }

        public List<PDF> PDFToClone { get; set; }

    }

The pdf class is:

public class PDF
    {
        public int ID{ get; set; }

        public bool Selected{ get; set; }

    }

I am trying to create a list of radiobuttons for the user to select 1 and this sets the selected property on the PDF element in the PDFToClone list.

Html.RadioButtonFor(model => Model.UIClonePDFDetail[Model.UIClonePDFDetail.IndexOf(stageSelection)].Selected, "stageSelection")

But this allows nultiple selection of radio buttons. I can see why this is happening, because the name elements of the Html tags are different. Is there a way of forcing them into the same family?

Était-ce utile?

La solution

RadioButtonFor contains an overload that accepts HTML attributes. You can create an anonymous type and include the Name attribute like this:

Html.RadioButtonFor(model => Model.UIClonePDFDetail[Model.UIClonePDFDetail
          .IndexOfstageSelection)].Selected, "stageSelection", new { Name = "grp" })

Autres conseils

Please try to use:

Html.RadioButtonFor(model => 
    Model.UIClonePDFDetail[Model.UIClonePDFDetail.IndexOf(stageSelection)].Selected,
    "stageSelection")

instead of:

Html.CheckBoxFor(model => 
    Model.UIClonePDFDetail[Model.UIClonePDFDetail.IndexOf(stageSelection)].Selected, 
   "stageSelection")

You could try to pass htmlAttributes with the group of the radiobutton, with something like:

@Html.RadioButtonFor(model => Model.UIClonePDFDetail[Model.UIClonePDFDetail
      .IndexOfstageSelection)].Selected, "stageSelection", new { Group = "A" })
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top