使用 MVC 框架,我创建了一个视图来将 Mentee 添加到我的 Mentee 数据库表中。虽然我能够输入与我的学员相关的所有信息,但我似乎无法弄清楚如何从我的学校下拉框中选择任何值并将 id 值输入到交叉引用表mentee_school注意:有已经在学校下拉表中的值。例如:
表关系:
受指导者视图模型:
public class MenteeViewModel
{
public mentee mentee { get; set; }
public guardian guardian { get; set; }
public address address { get; set; }
public email email { get; set; }
public user users { get; set; }
public phnumber phnumber { get; set; }
public econtact econtact { get; set; }
[Display(Name = "School System")]
public int SelectedSSystemId { get; set; }
public IEnumerable<SelectListItem> SSystemItems { get; set; }
[Display(Name = "School")]
public int SelectedSchoolId { get; set; }
public IEnumerable<SelectListItem> SchoolItems { get; set; }
}
受指导者观点:
<div class="editor-label">
@Html.LabelFor(model => model.SelectedSchoolId)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.SelectedSchoolId,(IEnumerable<SelectListItem>)ViewBag.school_ids, "--Select One--")
</div>
指导者控制器
[HttpPost]
public ActionResult Create(ViewModels.MenteeViewModel menteeViewModel )
{
if (ModelState.IsValid)
{
menteeViewModel.mentee.addresses.Add(menteeViewModel.address);
menteeViewModel.mentee.guardians.Add(menteeViewModel.guardian);
menteeViewModel.mentee.econtacts.Add(menteeViewModel.econtact);
menteeViewModel.mentee.emails.Add(menteeViewModel.email);
//Below statement doesn't work with current model
menteeViewModel.mentee.schools.Add(menteeViewModel.SelectedSchoolId);
db.mentees.Add(menteeViewModel.mentee);
db.SaveChanges();
return RedirectToAction("Index");
}
return Create();
}
我的问题是如何将下拉框中所选学校的值和学员 id 保存到 mentee_school 交叉引用表中?