0

I tried to pass the value of Gender property in my model called "Student" to @Html.RadioButtonFor(). I have defined Gender property is "Male" or "Female" before my Controller passed it to View. But when the page was displayed there was only one RadioButton checked. Was I wrong something?. Please help me! This is my Model class:

public class Student
{
    public int StudentId { get; set; }
    [Display(Name ="Name")]
    public string StudentName { get; set; }
    public int Age { get; set; }

    public string Gender { get; set; }

    public string Description { get; set; }
    public bool isNewlyEnrolled { get; set; }

}

This is my Controller code:

public class StudentController : Controller
{
    // GET: Student
    public ActionResult Index()
    {
        List<Student> lstStudents = new List<Student> {
            new Student { StudentId=1,StudentName="Anna",Age=18,Description="QQQQQQQQQQQQQQ",isNewlyEnrolled=true,Gender="Female"},
            new Student { StudentId=2,StudentName="Lance",Age=18,Description="QQQQQQQQQQQQQQ",Gender="Male"},
            new Student { StudentId=3,StudentName="Mc Holand",Age=18,Description="QQQQQQQQQQQQQQ",Gender="Male"},
            new Student { StudentId=4,StudentName="Michelle",Age=18,Description="QQQQQQQQQQQQQQ",isNewlyEnrolled=true,Gender="Female"},
            new Student { StudentId=5,StudentName="Michael",Age=18,Description="QQQQQQQQQQQQQQ",Gender="Male"},
            new Student { StudentId=6,StudentName="Danny",Age=18,Description="QQQQQQQQQQQQQQ",Gender="Male"},
        };
        return View(lstStudents);
    }
}

This is my View code:

 <table class="table" style="border:1px solid gray">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.StudentName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Age)
        </th>
        <th>
            Description
        </th>
        <th>
            IsNewlyRolled
        </th>
        <th>
            Gender
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.StudentName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Age)
        </td>
        <td>
            @Html.TextAreaFor(i=>item.Description,new {@style="color:red" })
        </td>
        <td>
            @Html.CheckBoxFor(i=>item.isNewlyEnrolled)
        </td>
        <td>
            Male:@Html.RadioButtonFor(m=>item.Gender,"Male",new { @id="item"+item.StudentId})
            Female:@Html.RadioButtonFor(m=>item.Gender,"Female",new { @id="item"+item.StudentId})

        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.StudentId }) |
            @Html.ActionLink("Details", "Details", new { id=item.StudentId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.StudentId })
        </td>
    </tr>
}

</table>

Thank you in advance!

ANJYR
  • 2,583
  • 6
  • 39
  • 60
binh nguyen
  • 127
  • 1
  • 13
  • You cannot generate form controls for a collection using a `foreach` loop - use a `for` loop or `EditorTemplate` (refer the dupe). And only one is selected because every radio button has `name="item.Gender"` - i.e there is only one group –  Sep 15 '17 at 08:16
  • @StephenMuecke: Thank you so much, I did change my foreach loop into for loop and my problem was solved. One more thing, Why we use for loop instead of foreach?. I have just learned MVC 5 in Ebook for one week and i saw almost everyone used foreach loop instead of for loop. – binh nguyen Sep 15 '17 at 08:34
  • No one uses a `foreach` to generate form controls :). And read the dupe to understand why (and accept it to close this out) –  Sep 15 '17 at 08:35
  • Thank you for your help! Have a beautiful day. – binh nguyen Sep 15 '17 at 08:36

0 Answers0