【问题标题】:List of Checkbox returns false even when checked in asp.net core razor pages即使在 asp.net core razor 页面中选中,复选框列表也会返回 false
【发布时间】:2020-12-29 10:40:22
【问题描述】:

学生考勤班

 public class StudentAttendance
{
    public int Id { get; set; }
    public int StudentID { get; set; }
    public bool IsPresent { get; set; }

}

查看我的出勤表表格

 @foreach (var student in Model.Students)
          {
            <tr>
                <td> @student.FullName</td>
                <td><input type="hidden" asp-for="HiddenID" id="id" value="@student.StudentID"/></td>
                <td ><label class="col-form-label"><input type="checkbox" name="IsPresent" ></label></td> </tr> }

出勤页面模型

 [BindProperty]
    public StudentAttendance studentAttendance { get; set; }

    [BindProperty]
    public List<int> StudentAttendanceList { get; set; }

    [BindProperty(SupportsGet = true)]
    public int? SearchClassID { get; set; }


    [BindProperty]
    public List<int> HiddenID { get; set; }

    [BindProperty]
    public bool IsPresent { get; set; }


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

出勤率()

foreach (var id in HiddenID)
        {
            Conn.StudentAttendance.Add(new StudentAttendance
            {
                StudentID = id,
                IsPresent = IsPresent,
            });
        }

当我提交并保存时,即使选中,IsPresent 值仍然为假

【问题讨论】:

    标签: asp.net asp.net-core razor entity-framework-core


    【解决方案1】:

    由于表单中的 IsPresent 并没有绑定到 Model 上,而模型中 IsPresent 的默认值为 false。最后,每次都得到假。通过JavaScript函数动态改变checkbox中的值,可以将对应选中的id提交到后台。

    在 Attendance.cshtml 中

    <form method="post">
        <table>
            @foreach (var student in Model.Students)
            {
                <tr>
            
                    <td> @student.FullName </td>
                    <td><input type="hidden" asp-for="HiddenID" id="id" value="@student.StudentID" /></td>
                    <td>
                        <label class="col-form-label">
                            <input type="checkbox" id="IsPresent" name="isPresent" value="false" onchange="changePresent(event,@student.StudentID)">
                        </label>
                    </td>
                </tr>
            }
        </table>
        <input type="submit" name="name" value="sub" />
    </form>
    @section Scripts{ 
    <script>
        function changePresent(e,id) {
            if (e.target.value=='true') {
                e.target.value = false
            }
            else {
                console.log('-=-=')
                e.target.value = id
            }
        }
    </script>
    }
    

    然后,在post方法中,获取被检查的id,并判断该id是否被检查。

    public void OnPost(int[] isPresent)
            {
                foreach (var id in HiddenID)
                {
                    new StudentAttendance
                    {
                        StudentID = id,
                        IsPresent = id == isPresent[id - 1],
                    };
                    /*Conn.StudentAttendance.Add(new StudentAttendance
                    {
                        StudentID = id,
                        IsPresent = IsPresent,
                    });*/
                }
            }
    

    结果:

    【讨论】:

    • 非常感谢Yiyi You,我真的很感激,但我得到一个异常错误:IndexOutOfRangeException: Index was outside the bounds of the array
    • 如果您能进一步帮助我,我将不胜感激我收到 IndexOutOfRangeException: Index was outside the bounds of the array 错误
    • 我认为你在案例中提到的问题可以通过代码解决,如果你有一个新的异常,你可以创建一个新线程,调试并告诉你从哪里得到这个异常,以及哪些数据你通过得到异常。
    猜你喜欢
    • 1970-01-01
    • 2018-04-02
    • 2020-09-06
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    • 2021-12-26
    • 1970-01-01
    相关资源
    最近更新 更多