为列表中访问选定的下拉Id asp.net mvc的每个记录保存一个项目
本文关键字:保存 记录 项目 一个 mvc asp 访问 列表 Id net | 更新日期: 2023-09-27 18:10:08
我有一个讲师,事件和一个名为instructovents的表,用于保存哪些教师可能会参加一个事件。
我已经生成了一个视图,其中列出了教师和每个教师旁边的下拉列表,以选择是,否或可能参加该活动。
这是它的控制器:
public ViewResult PreRegister(int eventId)
{
//Find Event
var selectedEvent = db.Events.FirstOrDefault(t => t.EventId == eventId);
//Instructors List
IEnumerable<Instructor> currentInstructors;
//Generate a list of the current Instructors
using (ApplicationDbContext context = new ApplicationDbContext())
{
currentInstructors = context.Instructors
.Where(s => s.Left < DateTime.Now)
.OrderBy(s => s.LastName)
.ToList();
}
EventPreRegisterViewModel viewModel = new EventPreRegisterViewModel
{
EventId = selectedEvent.EventId,
EventName = selectedEvent.Name,
InstructorRegisterListViewModels =
currentInstructors.Select(
x => new InstructorRegisterListViewModel { InstructorId = x.InstructorId, Name = x.FirstName + " " + x.LastName, Attendances = db.Attendances.Where(t => t.Dormant == false && t.AttendanceId == 1 || t.AttendanceId == 2 || t.AttendanceId == 3).ToList() }).ToArray()
};
return View(viewModel);
}
视图@model YFA.ViewModels.EventPreRegisterViewModel
@{
ViewBag.Title = "Pre Register for Event";
}
<h2>Pre Register for @Html.DisplayFor(model => model.EventName)</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.EventId)
<h3>Instructors</h3>
<div class="col-md-offset-1">
@for (int i = 0; i < Model.InstructorRegisterListViewModels.Count(); i++)
{
<div class="form-group">
<div class="row">
@Html.HiddenFor(x => Model.InstructorRegisterListViewModels[i].InstructorId)
<div class="col-md-2">
@Html.LabelFor(x => Model.InstructorRegisterListViewModels[i].Name, Model.InstructorRegisterListViewModels[i].Name)
</div>
<div class="col-md-2">
@Html.DropDownListFor(model => model.InstructorRegisterListViewModels[i].AttendanceId, new SelectList(Model.InstructorRegisterListViewModels[i].Attendances, "AttendanceId", "AttendanceName", 0), "Please Select", new { @class = "form-control" })
</div>
</div>
</div>
}
</div>
<div class="form-group">
<div class="col-md-offset-1 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to Event List", "Index")
</div>
这是我对post方法的尝试,该方法在InstructorEvent表中为每个教练添加了一条记录,但目前我已经手动编写了AttendanceId,而不是从下拉列表中引用所选Id。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult PreRegister(EventPreRegisterViewModel viewModel)
{
// Get list of instructors
IEnumerable<int> instructorIds = viewModel.InstructorRegisterListViewModels.Select(x => x.InstructorId);
// Find the event
Event eventSel = db.Events.Find(viewModel.EventId);
// Copy instructors in the event
IEnumerable<InstructorEvent> instructorEvents = eventSel.InstructorEvents.ToList();
// Remove previous instructors
foreach (InstructorEvent previousInstructor in instructorEvents)
{
db.InstructorEvents.Remove(previousInstructor);
}
// Add new club interests
foreach (int InstructorId in instructorIds)
{
Instructor instructor = db.Instructors.Find(InstructorId);
//THIS IS THE SECTION I'M STRUGGLING WITH
var newInstructorEvent = new InstructorEvent { EventId = eventSel.EventId, InstructorId = instructor.InstructorId, AttendanceId = 1};
db.InstructorEvents.Add(newInstructorEvent);
}
// Save new club interests
db.SaveChanges();
return RedirectToAction("Index");
}
为什么不循环遍历视图模型?
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult PreRegister(EventPreRegisterViewModel viewModel)
{
// Find the event
var eventSel = db.Events.Find(viewModel.EventId);
// Remove previous instructors
foreach (var previousInstructor in eventSel.InstructorEvents)
{
db.InstructorEvents.Remove(previousInstructor);
}
foreach (var model in viewModel.InstructorRegisterListViewModels)
{
var newInstructorEvent = new InstructorEvent { EventId = eventSel.EventId, InstructorId = model.InstructorId, AttendanceId = model.AttenanceId };
db.InstructorEvents.Add(newInstructorEvent);
}
// Save new club interests
db.SaveChanges();
return RedirectToAction("Index");
}
我将控制器修改为以下内容,如果选择了yes或maybe,则为AttendanceId保存不同的值。然而,我无法想象这是一种正确或优雅的方式?
[ValidateAntiForgeryToken]
public ActionResult PreRegister(EventPreRegisterViewModel viewModel)
{
// Find the event
Event eventSel = db.Events.Find(viewModel.EventId);
// Get list of instructors with yes selected
IEnumerable<int> instructorYesIds = viewModel.InstructorRegisterListViewModels.Where(x => x.AttendanceId == 1).Select(x => x.InstructorId);
// Get list of instructors with maybe selected
IEnumerable<int> instructorMaybeIds = viewModel.InstructorRegisterListViewModels.Where(x => x.AttendanceId == 3).Select(x => x.InstructorId);
// Copy instructors in the event
IEnumerable<InstructorEvent> instructorEvents = eventSel.InstructorEvents.ToList();
// Remove previous instructors
foreach (InstructorEvent previousInstructor in instructorEvents)
{
db.InstructorEvents.Remove(previousInstructor);
}
// Add those who said yes
foreach (int InstructorId in instructorYesIds)
{
Instructor instructor = db.Instructors.Find(InstructorId);
var newInstructorEvent = new InstructorEvent { EventId = eventSel.EventId, InstructorId = instructor.InstructorId, AttendanceId = 1 };
db.InstructorEvents.Add(newInstructorEvent);
}
// Add those who said maybe
foreach (int InstructorId in instructorMaybeIds)
{
Instructor instructor = db.Instructors.Find(InstructorId);
var newInstructorEvent = new InstructorEvent { EventId = eventSel.EventId, InstructorId = instructor.InstructorId, AttendanceId = 3 };
db.InstructorEvents.Add(newInstructorEvent);
}
// Save new club interests
db.SaveChanges();
return RedirectToAction("Index");
}