следуйте этому подходу, это будет полезно и тренировки для вашего случая. вот ссылка Кроме того, внутри вашего контроллера следуйте этому коду
[HttpGet]
public ActionResult Edit(int ID)
{
using (UniversityDbContext db=new UniversityDbContext())
{
ViewBag.SubjectID = new SelectList(db.Subjects, "SubjectID", "Code");
ViewBag.CourseId = new SelectList(db.Courses, "Id", "Code");
ViewBag.DepartmentId = db.Departments.ToList();
AllocateClassRoom allocateClassRoom = new AllocateClassRoom();
allocateClassRoom.DepartmentId = getAllocationDetails.DepartmentId;
allocateClassRoom.CourseId = getAllocationDetails.CourseId;
allocateClassRoom.SubjectID = getAllocationDetails.SubjectID;
allocateClassRoom.RoomId = getAllocationDetails.RoomId;
allocateClassRoom.date = getAllocationDetails.date;
allocateClassRoom.Day = getAllocationDetails.Day;
allocateClassRoom.From = getAllocationDetails.From;
allocateClassRoom.To = getAllocationDetails.To;
allocateClassRoom.StartTime = getAllocationDetails.StartTime;
allocateClassRoom.FinishTime = getAllocationDetails.FinishTime;
var allocateClassRooms = db.AllocateClassRooms.Include(a => a.Course).Include(a => a.Department).Include(a => a.Subject).Include(a => a.Room).ToList();
var getAllocationDetails = db.AllocateClassRooms.Where(s => s.Id == ID).FirstOrDefault();
Department department = new Department();
var departmnt = db.Departments.Where(s => s.ID == getAllocationDetails.DepartmentId).FirstOrDefault();
ViewData["DepartmentData"] = departmnt.Code;
var course = db.Courses.Where(s => s.Id == getAllocationDetails.CourseId).FirstOrDefault();
ViewData["CourseData"] = course.Code;
var subject = db.Subjects.Where(s => s.SubjectID == getAllocationDetails.SubjectID).FirstOrDefault();
ViewData["subjectData"] = subject.Code;
var room = db.Rooms.Where(x => x.Id == getAllocationDetails.RoomId).FirstOrDefault();
ViewData["roomData"] = room.RoomNo;
ViewData["DayValue"] = getAllocationDetails.Day;
return View(allocateClassRoom);
}
}
, как и ваш edit.cshtml следуйте этому коду
@model UniversityMvcApp.Models.AllocateClassRoom
@if (Errormessage != null)
{
<label>@Errormessage</label>
}
@if (allocatedMessage != null)
{
<label>@allocatedMessage</label>
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true)
<div class="form-group">
@Html.Label("Department Code", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@{
var dep = ViewData["DepartmentData"];
var cou = ViewData["CourseData"];
var sub = ViewData["subjectData"];
var roo = ViewData["roomData"];
var day = ViewData["DayValue"];
}
<select name="DepartmentId" id="DepartmentId">
<option>@dep</option>
@foreach (var department in ViewBag.DepartmentId)
{
<option value="@department.ID">@department.Code</option>
}
</select>
@Html.ValidationMessageFor(model => model.CourseId)
</div>
</div>
<div class="form-group">
@Html.Label("Course Code", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<select name="CourseId" id="CourseId"><option>@cou</option></select>
@Html.ValidationMessageFor(model => model.CourseId)
</div>
</div>
<div class="form-group">
@Html.Label("Subject Code", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<select name="SubjectID" id="SubjectID"><option>@sub</option></select>
@Html.ValidationMessageFor(model => model.SubjectID)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.RoomId, "Lecture Place", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<select name="RoomId" id="RoomId">
<option>@roo</option>
@foreach (var room in ViewBag.RoomId)
{
<option value="@room.Id">@room.RoomNo</option>
}
</select>
@Html.ValidationMessageFor(model => model.RoomId)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.date, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.date, new { @id = "Date" })
@Html.ValidationMessageFor(model => model.date)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Day, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<select name="Day" id="Day">
<option value="Saturday">Saturday</option>
<option value="Sunday">Sunday</option>
<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thrusday">Thrusday</option>
<option value="Friday">Friday</option>
</select>
@Html.ValidationMessageFor(model => model.Day)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.From, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.From, new { @class = "From" })
@Html.ValidationMessageFor(model => model.From)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.To, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.To, new { @class = "To" })
@Html.ValidationMessageFor(model => model.To)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Update" class="btn btn-success" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jquery")
<script src="//cdnjs.cloudflare.com/ajax/libs/timepicker/1.3.5/jquery.timepicker.min.js"></script>
<script src="~/Scripts/jquery-ui-1.12.1.js"></script>
<script>
$(function () {
$("#Date").datepicker({ dateFormat: 'dd-mm-yy' }).val;
});
$('.From,.To').timepicker({
timeFormat: 'h:mm p',
interval: 30,
minTime: '8',
maxTime: '6:00pm',
defaultTime: '8',
startTime: '8:00',
dynamic: false,
dropdown: true,
scrollbar: true
});
$(document).ready(function () {
$("#DepartmentId").change(function () {
var deptId = $("#DepartmentId").val();
$("#CourseId").empty();
$("#CourseId").append('<option value="">Select</option>');
var json = { DepartmentId: deptId };
$.ajax({
type: "POST",
url: '/AllocateClassRooms/GetCourseByDepartmentId',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(json),
success: function (data) {
$.each(data, function (key, value) {
$("#CourseId").append('<option value="' + value.Id + '">'
+ value.Code + '</option>');
});
}
});
});
});
$(document).ready(function () {
$("#CourseId").change(function () {
var courId = $("#CourseId").val();
$("#SubjectID").empty();
$("#SubjectID").append('<option value="">Select</option>');
var json = { CourseId: courId };
$.ajax({
type: "POST",
url: '/AllocateClassRooms/GetSubjectByCourseId',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(json),
success: function (data) {
$.each(data, function (key, value) {
$("#SubjectID").append('<option value="' + value.SubjectID + '">' + value.Code + '</option>');
});
}
});
});
});
</script>
}
</div>
я думаю, что согласноваш код, он должен тренироваться для вас