38818

MVC RadioButtonFor group

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?

Answer1:

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

Answer2:

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

Answer3:

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

Recommend

  • Hide/Display a input field based on value from HtmlRadioButtonFor and make it mandatory if display
  • Using Ajax.BeginForm in MVC4
  • ASP.NET MVC3. How to set validation to require one of two fields depending on bool value of the mode
  • Are there multiple ways to group radio buttons in Windows Forms?
  • Can I make WPF RadioButtons look like CheckBoxes?
  • InnoSetup, Convert tasks to Radiobuttons?
  • RadioButtonFor not selecting value in editor templates
  • Each Radiobutton for each form or 1 Form for all radiobuttons?
  • How to initialize context? [closed]
  • Granting permissions to Azure Active Directory Web Application automatically
  • In matplotlib, how do you change the fontsize of a single figure?
  • Image map in Flex
  • Who propagate bugfixes across branches (corporate development)?
  • Entity Framework Code First TPC Inheritance Self-Referencing Child Class
  • MySQL Order by column = x, column asc?
  • ORA-29908: missing primary invocation for ancillary operator
  • How to get next/previous record number?
  • Comma separated Values
  • How to get icons for entities from eclipse?
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • How do you join a server to an Active Directory (domain)?
  • how does django model after text[] in postgresql [duplicate]
  • Turn off referential integrity in Derby? is it possible?
  • How does Linux kernel interrupt the application?
  • Authorize attributes not working in MVC 4
  • Add sale price programmatically to product variations
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • JaxB to read class hierarchy
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How do I use LINQ to get all the Items that have a particular SubItem?