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?

Was it helpful?

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

OTHER TIPS

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" })
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top