发布RadioButtons MVC列表列表

时间:2018-08-02 18:15:17

标签: c# asp.net-mvc entity-framework model-view-controller radio-button

我有一个出勤计划,我想将学生分配给AttendanceTakers。我正在使用一个表,其中标题是AttendanceTakers,行是Student,每个单元格都有一个RadioButton。它基本上是RadioButtons的双重数组。我的问题是我无法发布它。

我的AttendanceTaker班

public class SessionAttendanceTaker
    {

        public int Id { get; set; }

        [ForeignKey("Session")]
        public int SessionId { get; set; }
        public Session Session { get; set; }

        [Display(Name="Attendance Taker")]
        [ForeignKey("User")]
        public string AttendanceTakerId { get; set; }
        [Display(Name = "Attendance Taker")]
        public User User { get; set; }

        public List<Student> Students { get; set; }
    }

以及在课程中的学生

public class StudentSession 
    {

        public int Id { get; set; }

        [ForeignKey("Session")]
        [DisplayName("Session")]
        public int SessionId { get; set; }
        public Session Session { get; set; }

        [ForeignKey("Student")]
        [DisplayName("Student")]
        public int StudentId { get; set; }
        public Student Student { get; set; }

        [DisplayName("Credits Awarded")]
        public int Credit { get; set; }
}

学生班

public class Student 
    {
        public int Id { get; set; }


        [ForeignKey("User")]
        public string UserId { get; set; }

        [DisplayName("Name")]
        public virtual User user { get; set; }

        public Student() 
        {

        }
  }

视图

@using (Html.BeginForm())
{

    <div class="form-horizontal">
        <table>
            <thead>
                <tr>
                    <th> Name &nbsp; &nbsp;</th>

                    @{
                        foreach (var attendanceTaker in Model.SessionAttendanceTakers)
                        {
                            <th>@attendanceTaker.User.LastName, @attendanceTaker.User.FirstName &nbsp; &nbsp;</th>
                        }
                    }

                </tr>
            </thead>
            <tbody>

                @{
                    //See https://stackoverflow.com/questions/7667495/mvc-radiobuttons-in-foreach to try and clean the foreach
                    foreach (var studentSession in Model.StudentSessions)
                    {

                        <tr>
                            <td>
                                @studentSession.Student.User.LastName, @studentSession.Student.User.FirstName
                            </td>
                            @foreach (var attendanceTaker in Model.SessionAttendanceTakers)
                            {
                                @Html.EditorFor(Model => Model.SessionAttendanceTakers, "StudentsToAttendanceTakersModel", "" + studentSession.StudentId, new { htmlAttributes = new { @class = "form-control" } })
                            }

                        </tr>
                    }
                }
            </tbody>
        </table>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Assign" class="btn btn-default" />
            </div>
        </div>
    </div>
}

和EditorTemplate

@model IEnumerable<SessionAttendanceTaker>
@using Attendance.Models


<td>
    @Html.RadioButtonFor(model => model, new { htmlAttributes = new { @class = "form-control" } })
</td>

顺便说一句,我很想摆脱post的要求,但是由于我不知道要多少人才能上学,直到运行时我都不知道该怎么做除了将它们移至编辑器之外,我看不到这一点。

也是控制器

[HttpPost]
        public ActionResult Assign(StudentsToAttendanceTakersModel model)
        {

            return RedirectToAction("Index");
        }

我在回程表上有一个断点,而AttenanceTakers为空,并且学生会话的计数为0。

另外,使用FormCollection

public ActionResult Assign(FormCollection o)

仅给我单击了RadioButton的学生,但没有给我AttendanceTaker。如果需要更多信息,请告诉我。谢谢。

编辑 型号

public class StudentsToAttendanceTakersModel
    {
        public IEnumerable<StudentSession> StudentSessions { get; set; }
        public IEnumerable<SessionAttendanceTaker> SessionAttendanceTakers { get; set; }

        public StudentsToAttendanceTakersModel() { }
    }

1 个答案:

答案 0 :(得分:0)

您正在创建与模型不相关的单选按钮,并试图将它们绑定到复杂的对象(SessionAttendanceTaker)-单选按钮回发一个简单值(但您不会甚至为单选按钮提供了一个有效值-RadioButtonFor()的第二个参数是value)。

您正在编辑数据,因此您应该首先创建表示要在视图中显示的视图模型。

public class StudentVM
{
    public int ID { get; set; }
    public string Name { get; set; }
    [Required(ErrorMessage = "Please select an attendance taker")]
    public int? SelectedAttendanceTaker { get; set; }
}
public class AttendanceTakerVM
{
    public int ID { get; set; }
    public string Name { get; set; }
}
public class StudentAttendanceTakersVM
{
    public List<StudentVM> Students { get; set }
    public IEnumerable<AttendanceTakerVM> AttendanceTakers { get; set; }
}

这样您的视图就会

@model StudentAttendanceTakersVM
....
@using (Html.BeginForm())
{
    <table>
        <thead>
            <tr>
                <th>Student</th>
                @foreach(var taker in Model.AttendanceTakers)
                {
                    <th>@taker.Name</th>
                }
                <th></th>
            </tr>
        </thead>
        <tbody>
        @for(int i = 0; i < Model.Students.Count; i++)
        {
            <tr>
                <td>
                    @Model.Students[i].Name
                    @Html.HiddenFor(m => m.Students[i].ID)
                    @Html.HiddenFor(m => m.Students[i].Name)
                </td>
                @foreach(var taker in Model.AttendanceTakers)
                {
                    <td>@Html.RadioButtonFor(m => m.Students[i].SelectedAttendanceTaker, taker.ID, new { @class = "form-control" })</td>
                }
                <td>@Html.ValidationMessageFor(m => m.Students[i].SelectedAttendanceTaker)</td>
            </tr>
        }
        </tbody>
    </table>
    <input type="submit" ... />
}

然后,您的GET方法将初始化您的视图模型实例,并将其传递给视图,例如,使用'Create'方法

public ActionResult Create()
{
    var students = db.Students.Select(x => new StudentVM
    {
        ID = x.Id,
        Name = x.User.FirstName + " " + x.User.LastName // adjust as required
    }).ToList();
    var attendanceTakers = db.SessionAttendanceTakers.Select(x => new AttendanceTakerVM
    {
        ID = x.Id,
        Name = x.User.FirstName + " " + x.User.LastName // adjust as required    
    });
    StudentAttendanceTakersVM model = new StudentAttendanceTakersVM
    {
        Students = students,
        AttendanceTakers = attendanceTakers
    };
    return View(model);
}

POST方法将

public ActionResult Create(StudentAttendanceTakersVM model)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }
    // loop through model.Students to get the ID of the Student and its selected AttendanceTaker
    // initialize the data models and save to the database
    return RedirectToAction("Index");
}