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}; –