简体   繁体   English

MVC RadioButtonFor组

[英]MVC RadioButtonFor group

I have a class for a PDF 我有一个PDF类

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

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

    }

The pdf class is: pdf类是:

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. 我正在尝试创建一个radiobuttons列表供用户选择1,这将在PDFToClone列表中的PDF元素上设置selected属性。

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. 我可以看到为什么会发生这种情况,因为Html标签的名称元素是不同的。 Is there a way of forcing them into the same family? 有没有办法强迫他们进入同一个家庭?

RadioButtonFor contains an overload that accepts HTML attributes. RadioButtonFor包含一个接受HTML属性的重载。 You can create an anonymous type and include the Name attribute like this: 您可以创建一个匿名类型并包含Name属性,如下所示:

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

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: 您可以尝试使用radiobutton的组传递htmlAttributes,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM