【问题标题】:Retrieving student attendance data from form to controller将学生出勤数据从表单检索到控制器
【发布时间】:2017-05-26 10:53:30
【问题描述】:

我想将学生出勤率从剃刀视图检索到控制器。在一行中,我想访问单选按钮值。并且有很多行。如何一次访问所有行数据并将其发送到控制器?

我的 Razor 视图是:

 <table class="table">
                        <thead class="thead-inverse">
                            <tr>

                                <th>Full Name</th>
                                <th>Roll No</th>
                                <th>Status</th>

                                @*  <th>Status</th>*@
                            </tr>
                        </thead>
                        <tbody>
                            @using (@Html.BeginForm("Save", "Attendance", FormMethod.Post, new { @class = "form-horizontal" }))
                            {

                                foreach (var student in Model.Students)
                                {
                                    <tr>
                                        <td>@student.Name</td>
                                        <td>@student.RollNo</td>
                                        <td>

                                            <div data-toggle="buttons">
                                                <label class="btn btn-success active">
                                                    <input type="radio" name="options" id="1" autocomplete="off" checked> Present
                                                </label>
                                                <label class="btn btn-danger">
                                                    <input type="radio" name="options" id="2" autocomplete="off" checked> Absent
                                                </label>
                                                <label class="btn btn-primary">
                                                    <input type="radio" name="options" id="3" autocomplete="off" checked> On Leave
                                                </label>
                                                <label class="btn btn-warning">
                                                    <input type="radio" name="options" id="4" autocomplete="off" checked> Short Leave
                                                </label>
                                            </div>
                                        </td>
                                    </tr>

                                }
                                <div class="form-group">
                                    <div class="col-lg-10 col-lg-offset-2">
                                        <button type="submit" class="btn btn-success">Submit</button>
                                    </div>
                                </div>

                            }
                        </tbody>
                    </table>

我附上了我网页的屏幕截图,以便您可以看到我想要实现的目标。

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 razor model-view


    【解决方案1】:

    不确定您是否尝试过在 HttpPost 中传回 FormCollection。这是一个简单的例子:

        [HttpPost]
        public ActionResult Save(FormCollection form)
        {
            //form variable should contain data from form controls. 
            return View();
        }
    

    【讨论】:

      【解决方案2】:

      您可以将学生列表传递给控制器​​:

      [HttpPost]
      public ActionResult Save(List<Students> students)
      {
          //here you can manipulate that list 
          return View();
      }
      

      确保在视图中传递所有提交或创建“全选”按钮的学生

      【讨论】:

        猜你喜欢
        • 2020-06-12
        • 2013-01-13
        • 2012-07-02
        • 1970-01-01
        • 1970-01-01
        • 2019-01-21
        • 2019-06-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多