0
我试图从学生注册表格中插入数据到数据库中。进入学生表的数据和进入应用表的数据的一部分。输入进入STUDENT表的数据以及从APPLIED表中下拉列表中选择的课程。我无法从选定的课程中取得CourseID。通过选择下拉列表将数据插入数据库
问题是在该代码中的控制器:
var apply = new Applied { StudentID = application.StudentID, CourseID = application.CourseID };
在调试模式下它没有显示在CourseID任何数据。
以下是我的代码。
视图模型:
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; } //PK
public int CourseID { get; set; } //FK
}
控制器:
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)
{
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, AppliedDate = application.AppliedDate };
var apply = new Applied { StudentID = application.StudentID, CourseID = application.CourseID };
if (ModelState.IsValid)
{
using (var db = new RegistrarsContext())
db.Students.Add(student);
db.Applied.Add(apply).ApplicationStatus = Status.Applied;
db.SaveChanges();
return RedirectToAction("Index", "Student");
}
return View();
}
以下是VIEW:
@model StudentsRegistration.ViewModels.Application2
@{
ViewBag.Title = "Student Appication";
Layout = "~/Views/Shared/_Layout2.cshtml";
}
<h2>Student Application Form</h2>
@using (Html.BeginForm("Create", "Application2", 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>
<br /><br />
</div>
<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>
请发表您的视图代码也 – Vijai
这一段代码不在视图中。我应该包括这个。你能解释一下吗? –
不,它仍然没有传递任何值:var apply = new Applied {StudentID = application.StudentID,CourseID = application.CourseID}; –