0

I have code similar to the structure below, the naming attributes have been designed in a way to deal with mvc model binding.

At the moment, with the code sampled below - I want 4 radio buttons - 2 for each students Forename and Surname. Where one radio button can be checked for each student e.g. student1 firstname and student2 surname.

At the moment - with my inability to adjust the name attribute of the input - all radio buttons are treated as the same group, as a result I can select a maximum of one radio button.

Any ideas, thanks.

public List<Student> Students = new List<Student>();
public class Student
{
    public bool IsMale { get; set; }
    public bool IsFemale { get; set; }
    public bool IsOther { get; set; }

}
<div class="form-group-1">
       <input type="radio" name="Students[0].IsMale" value="male"> Male
       <input type="radio"name="Students[0].IsFemale"value="female">Female
       <input type="radio" name="Students0].IsOther" value="other"> Other
</div>

<div class="form-group-2">
      <input type="radio" name="Students[1].IsMale" value="male"> Male
      <input type="radio"name="Students[1].IsFemale"value="female">Female
      <input type="radio" name="Students[1].IsOther" value="other"> Other
</div>

<div class="form-group-3">
      <input type="radio" name="Students[2].IsMale" value="male"> Male
      <input type="radio"name="Students[2].IsFemale"value="female">Female
      <input type="radio" name="Students[2].IsOther" value="other"> Other
</div>
J.Doe
  • 61
  • 4
  • Does this help? https://stackoverflow.com/a/17638187/3608792 – Dan Wilson Sep 11 '18 at 19:40
  • You can use taghelpers `` to retrieve data from models. – Llazar Sep 11 '18 at 19:42
  • @DanWilson unfortunately not, thanks though – J.Doe Sep 11 '18 at 19:52
  • Can you explain why? How are you rendering your markup? – Dan Wilson Sep 11 '18 at 19:53
  • The code you show creates 4 radio button groups, not one (you can select all of them despite what you claim). And your radio buttons do not even have a `value` so its not clear what you are trying to do with this code. What is it you are trying to bind to –  Sep 11 '18 at 23:16
  • I'm binding to a List - I can select one radio button at most. @StephenMuecke – J.Doe Sep 15 '18 at 14:43
  • The code in your question gives each radio button a different `name` attribute which means you can select all of them (and cannot un-select any). –  Sep 15 '18 at 22:44
  • @StephenMuecke As stated, that doesn't seem to be the case. Hopefully i'm not wrong here, In any case where a List is posted back - indexing is used to bind properties back to its correct the object, therefore the name attribute will always be unique because of the index that's used. In that case how would it be radio button name grouping be done? – J.Doe Sep 16 '18 at 17:25
  • Your not making sense. Radio buttons are for selecting the value of a property from multiple options. What options are you expect to choose from for a Students Surname? –  Sep 16 '18 at 21:56
  • @StephenMuecke I have updated my code example in the main question - hopefully it provides more context/clears things up. With the code presented - Student[0] can select all 3 radio button options because all 3 options have different name attribute values. How I would like it to behave is, Student[0] can only make one selection - i.e. a student is either male, female or other. – J.Doe Sep 16 '18 at 22:53
  • You cannot bind a string containing "male" or "female" or "other" to a `bool`. You need to bind to a single property - say `public string Gender { get; set; }`, and you radio buttons will be ` Male`, ` Female` etc –  Sep 16 '18 at 23:00

0 Answers0