使用三个单选按钮在ASP.Net中编辑多条记录

时间:2016-02-17 20:59:39

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我尝试实施学生出勤申请,其中教师可以给学生三种状态之一(出席,缺席,未记录)。在教师登录后,他可以去上课并更改每个学生的状态并保存更改 enter image description here

所以我只需修改https://stackoverflow.com/a/18859712/3254920

模型

public class Student
{
    public string Id { get; set; }
    [Display(Name = "Full Name ")]
    public string FullName { get; set; }
    [Display(Name = "Student Status")]
    public int StudentStatus { get; set; }
    public List<StudentStatus> StudentStatusList { get; set; }
}
public class StudentStatus
{
    public string ID { get; set; }
    public string Type { get; set; }
}

我的控制器

public ActionResult Index()
{
    List<Student> model=new List<Student>();
    for (int i = 0; i < 5; i++)
    {
        var student = new Student
        {
            Id = i.ToString(),
            FullName = "Asd" +i,
            StudentStatusList = new List<StudentStatus>
        {
            new StudentStatus{ID="1" , Type = "Present"},
            new StudentStatus{ID="2" , Type = "Absent"},
            new StudentStatus{ID="3" , Type = "Unrecorded"}
        }
        };
        model.Add(student);
    }

    return View(model);
}
[HttpPost]
public ActionResult Index(List<Student> model)
{
    if (ModelState.IsValid)
    {
        //TODO: Save your model and redirect
    }
    return View(model);
}

视图

@model List<Models.Student>
    @{
        ViewBag.Title = "Index";
    }
    <h2>Index</h2>

    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        <table class="table table-hover table-condensed table-striped table-bordered">
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.FirstOrDefault().FullName)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.FirstOrDefault().StudentStatus)
                </th>
            </tr>

            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.FullName)
                    </td>
                    <td>
                        @foreach (var status in item.StudentStatusList)
                        {
                            @Html.RadioButtonFor(modelItem => item.StudentStatus, status.ID, new { @id = "Status" + item.Id+ status.ID, @Name = item.Id })
                            @Html.Label("Status" + item.Id+ status.ID, status.Type)
                        }
                    </td>
                </tr>
            }
        </table>
        <input type="submit" value="Save" class="btn btn-default" />
    }

运行应用程序后,我发现模型为空,我不知道为什么

enter image description here

我感谢任何帮助。

修改

我也尝试了以下内容,模型仍为null

@model List<Models.Student>
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <table class="table table-hover table-condensed table-striped table-bordered">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.FirstOrDefault().FullName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.FirstOrDefault().StudentStatus)
            </th>
        </tr>

        @for(int i = 0; i < Model.Count(); i++)
        {
            <tr>
                <td>
                    @Html.DisplayFor(m=>m[i].FullName)
                </td>
                <td>
                    @foreach (var status in Model[i].StudentStatusList)
                    {
                        <label>
                            @Html.RadioButtonFor(m => m[i].StudentStatus, status.ID, new { id = "" })
                            <span>@status.Type</span>
                        </label>
                    }
                </td>
            </tr>
        }
    </table>
    <input type="submit" value="Save" class="btn btn-default" />
}

编辑2 执行后的应用程序 enter image description here

编辑3

谢谢Stephen Muecke为了您的帮助,我只需重新启动计算机,每件事情都能正常运行

0 个答案:

没有答案