【问题标题】:How do i add a student to the attendance table如何将学生添加到出勤表
【发布时间】:2020-06-12 07:20:28
【问题描述】:

我想通过 Class_Schedule 控制器将学生添加到出勤表。为此,我创建了一个公共 ActionResult:

public ActionResult Register(int? id)           
{
  if (id == null)
  {
     return RedirectToAction("Index");
  }
  Class_Schedule class_Schedule = db.Class_Schedule.Find(id);
  if (class_Schedule == null)
  {
     return RedirectToAction("Index");
  }
  //This is the collects the class_schedule ID to make the attendance specific for each class ViewBag.CSid = id;
  ViewBag.studentID = new SelectList(db.Students, "StudentID", "Full_Name");
  ViewBag.instructorID = new SelectList(db.Instructors, "InstructorID", "Name");
  var attendances = db.Attendances;

  return View(attendances.ToList());
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register([Bind(Include = "AttendanceID,csID,InstructorID,StudentID")] Attendance attendance)
{
  try
  {
    if (ModelState.IsValid)
    {
      db.Attendances.Add(attendance);
      db.SaveChanges();
      //ViewBag.msg = "Instructor Added";
      return RedirectToAction("Register");
    }
    return View(attendance);
   }
   catch
   {
     return View(attendance);
   }
}

这是我的看法:

@model IEnumerable<BBM.Models.Attendance>

@{
    ViewBag.Title = "Register";
}

<h2>Class Schedule @ViewBag.CSid</h2>

@using (Html.BeginForm("Register","Class_Schedule", FormMethod.Post))
{
@Html.AntiForgeryToken()


<div class="form-group">
    @{
        var studentid = Model.Select(model => model.StudentID.ToString());
    }
    @Html.Label("StudentID", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownList("StudentID", null, htmlAttributes: new { @class = "form-control" })
    </div>
</div>

<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" value="Create" class="btn btn-default" />
    </div>
</div>
<h4>Student register</h4>
<table class="table">
    <tr>

        <th>
            Attendance ID
        </th>
        <th>
            Student ID
        </th>
        <th>
            Student Name
        </th>
        <th>
            Expiry Date
        </th>
    </tr>
    @if (Model != null)
    {

        foreach (var item in Model.Where(p => p.csID.Equals(ViewBag.csID)))
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.AttendanceID)
                </td>

                <td>
                    @Html.DisplayFor(modelItem => item.StudentID)
                </td>

                <td>
                    @Html.DisplayFor(modelItem => item.Student.Full_Name)
                </td>

                <td>
                    @if (item.Student.Payments != null && item.Student.Payments.Any(p => p.Expires > DateTime.Now))
                    {
                        @Html.DisplayFor(modelItem => item.Student.Payments.OrderByDescending(p => p.paymentID).First(p => p.Expires > DateTime.Now).Expires)
                    }
                    else
                    {
                        @Html.DisplayName("Expired");
                    }
                </td>

            </tr>
        }
    }
</table>
     @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
     }

这个视图有一个选择列表来选择你想添加的学生,但是学生 ID 没有进入参数并且 postMethod 没有发生

已经存在的那些用于测试目的,我是通过 sql server 完成的

【问题讨论】:

    标签: c# asp.net-mvc model-view-controller asp.net-mvc-5


    【解决方案1】:

    您只有一个指定的下拉 id 和 html 属性。您在填写ViewBag 时忘记将数据传递给下拉助手。为学生更新如下

    @Html.DropDownList("StudentID",htmlAttributes:new { @class = "control-label col-md-2" },selectList:new SelectList(ViewBag.studentID))
    

    请检查您是否在 post 方法中收到 studentId

    【讨论】:

    • 列表正在运行,学生显示,列表在控制器中,但我的问题是表在视图中没有更新,但在 MSQLSMS 中
    猜你喜欢
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多