从表单到控制器

时间:2017-05-26 10:53:30

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

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

我的剃刀观点是:

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

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

My Web Page View

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

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

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