MVC RadioButtonFor group

时间秒杀一切 提交于 2019-12-10 18:15:12

问题


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?


回答1:


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



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/13702777/mvc-radiobuttonfor-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!