gpt4 book ai didi

c# - 为访问所选下拉列表 Id asp.net mvc 的列表中的每条记录保存一个项目

转载 作者:太空宇宙 更新时间:2023-11-03 13:01:15 25 4
gpt4 key购买 nike

我有一张包含 Instructors、Events 和一个名为 InstructorEvents 的表,用于保存可能要参加事件的讲师。

我生成了一个 View ,其中列出了讲师和每个讲师旁边的下拉列表以选择是、否或可能参加该事件。

这是它的 Controller :

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");
}

最佳答案

为什么不只是循环遍历 View 模型?

[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");
}

关于c# - 为访问所选下拉列表 Id asp.net mvc 的列表中的每条记录保存一个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32250509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com