Multiple radio button groups in MVC 4 Razor

后端 未结 5 1679
耶瑟儿~
耶瑟儿~ 2020-12-03 06:33

I need to have multiple radio button groups in my form like this:
\"enter

I know

5条回答
  •  执笔经年
    2020-12-03 07:13

    Ok here's how I fixed this

    My model is a list of categories. Each category contains a list of its subcategories.
    with this in mind, every time in the foreach loop, each RadioButton will have its category's ID (which is unique) as its name attribue.
    And I also used Html.RadioButton instead of Html.RadioButtonFor.

    Here's the final 'working' pseudo-code:

    @foreach (var cat in Model.Categories)
    {
      //A piece of code & html here
      @foreach (var item in cat.SubCategories)
      {
         @Html.RadioButton(item.CategoryID.ToString(), item.ID)
      }    
    }
    

    The result is:

    
    

    Please note that I HAVE NOT put all these radio button groups inside a form. And I don't know if this solution will still work properly in a form.

    Thanks to all of the people who helped me solve this ;)

提交回复
热议问题