将值从模型的属性传递给@ Html.RadioButtonFor()

时间:2017-09-15 08:13:37

标签: razor asp.net-mvc-5

我尝试将名为“Student”的模型中的Gender属性值传递给@Html.RadioButtonFor()。在Controller将其传递给View之前,我已经定义了Gender属性为“Male”或“Female”。但是当显示页面时,只检查了一个RadioButton。我错了吗?请帮我!这是我的Model类:

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; }

}

这是我的控制器代码:

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

这是我的查看代码:

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

提前谢谢!

0 个答案:

没有答案