我有更多关于5个表,学生,课程,部门,申请,注册。
学生在网上申请特定课程,填写表格,提供个人详细信息,包括姓名,地址,联系方式等,并选择部门和特定课程,然后提交表格。
我无法提交该表格,换句话说无法插入学生输入的数据。以下是我的表格。
学生表:
public class Student
{
public int StudentID { get; set; } //PK
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
public string Address { get; set; }
public double PhoneNumber { get; set; }
public string Email { get; set; }
public DateTime AppliedDate { get; set; }
public virtual ICollection<Applied> Applications { get; set; }
}
课程表:
public class Course
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int CourseID { get; set; } //PK
public int DepartmentID { get; set; } //FK
public string CourseName { get; set; }
public string CoursCode { get; set; }
public int NfqLevel { get; set; }
public int CreditHrs { get; set; }
public DateTime StartDate { get; set; }
public DateTime FinishDate { get; set; }
public virtual Department Department { get; set; }
public virtual ICollection<Applied> Applications { get; set; }
}
}
APPLIED TABLE:
public class Applied
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int AppliedID { get; set; } //PK
public int StudentID { get; set; } //FK
public int CourseID { get; set; } //FK
public Status? ApplicationStatus { get; set; }
public virtual Student Student { get; set; }
public virtual Course Course { get; set; }
public virtual Enrolment Enrolment { get; set; }
}
MODELVIEW表:
public class Application2
{
public int StudentID { get; set; } //PK
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
public string Address { get; set; }
public double PhoneNumber { get; set; }
public string Email { get; set; }
public DateTime AppliedDate { get; set; }
public int? AppliedID { get; set; }
public int CourseID { get; set; }
}
跟进是控制器部分:
public class Application2Controller : Controller
{
private RegistrarsContext db = new RegistrarsContext();
public ActionResult Index()
{
return View();
}
public ActionResult DepartmentList()
{
var departments = db.Departments.OrderBy(x => x.DpName).ToList();
if (HttpContext.Request.IsAjaxRequest())
{
return Json(new SelectList(
departments,
"DepartmentID",
"DpName"), JsonRequestBehavior.AllowGet
);
}
return View(departments);
}
public ActionResult CourseList(int DepartmentID)
{
var courses = db.Courses.Where(x => x.DepartmentID == DepartmentID).ToList();
if (HttpContext.Request.IsAjaxRequest())
return Json(new SelectList(courses, "CourseID", "CourseName"), JsonRequestBehavior.AllowGet);
return View(courses);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Application2 application)
{
using (var db = new RegistrarsContext())
if (ModelState.IsValid)
{
var student = new Student { StudentID = application.StudentID, FirstName = application.FirstName, LastName = application.LastName, DateOfBirth = application.DateOfBirth, Address = application.Address, PhoneNumber = application.PhoneNumber, Email = application.Email };
var apply = new Applied { CourseID = application.CourseID, StudentID = application.StudentID };
db.Students.Add(student);
db.Applied.Add(apply);
}
db.SaveChanges();
return View();
}
以下是观看部分:
@model StudentsRegistration.ViewModels.Application2
@{
ViewBag.Title = "Student Appication";
}
<h2>Student Application Form</h2>
@using (Html.BeginForm(FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Student Details </h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" } <div class="form-group">
@Html.LabelFor(model => model.StudentID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.StudentID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StudentID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.StudentID)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DateOfBirth, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DateOfBirth)
@Html.ValidationMessageFor(model => model.DateOfBirth)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PhoneNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PhoneNumber)
@Html.ValidationMessageFor(model => model.PhoneNumber)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AppliedDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.AppliedDate)
@Html.ValidationMessageFor(model => model.AppliedDate)
</div>
</div>
<br>
<br>
<div class="form-group">
<label for="Departments">Departments</label>
<select id="Departments" name="Departments"></select>
</div>
<br /><br />
<div class="form-group">
<label for="Courses">Courses</label>
<select id="Courses" name="Courses"></select>
</div>
@section scripts {
<script type="text/javascript">
$(function () {
$.getJSON("/Application2/Departments/List", function (data) {
var items = "<option>---------------------</option>";
$.each(data, function (i, country) {
items += "<option value='" + country.Value + "'>" + country.Text + "</option>";
});
$("#Departments").html(items);
});
$("#Departments").change(function () {
$.getJSON("@Url.Action("CourseList", "Application2")?DepartmentID=" + $("#Departments").val(), function (data) {
var items = "<option>---------------------</option>";
$.each(data, function (i, state) {
items += "<option value='" + state.Value + "'>" + state.Text + "</option>";
});
$("#Courses").html(items);
});
});
});
</script>
}
<br /><br />
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>