我开发一个管理员工队伍的应用程序,我用Razor使用MVC4 Asp.net。MVC 4绑定多个模型asp剃须刀
在我的模型中,我有两个班级(谁是我的数据库中的表格)和业余(培训人员)。
在我的应用程序中,我可以创建一个“编组”,我想添加一个“形成”(培训师)列表,但我不知道我必须做什么。 我认为最好的解决方案是复选框列表,我成功地用foreach显示了我的复选框列表,但我不知道如何让选定复选框的结果传入我的控制器。
我看到很多使用“CheckBoxList”的教程,我也尝试使用,但是我使用ViewBag来填充它,但他们没有解释如何将它与viewbag一起使用。
现在我测试一个双列表框与两个按钮(添加和删除),但这是行不通的。
那么,有人可以帮助我找到并解释我必须怎么做,好的或最好的解决方案?
我很抱歉我的英语,我是一个法国女孩。
我的一个解决方案是这样的: 我的控制器:
public ActionResult Create()
{
ViewBag.formateurListe = (from unFormateur in db.salarie
where unFormateur.sFormateur == true
select unFormateur).AsEnumerable()
.Select(m => new SelectListItem
{
Text = m.sNom.ToString() + " " + m.sPrenom.ToString(),
Value = m.sId.ToString()
}).ToList();
return View();
}
[HttpPost]
public ActionResult Create(formation formation, IEnumerable<SelectList> formateurList)
{
if (ModelState.IsValid)
{
db.formation.Add(formation);
foreach (var unSal in formateurList)
{
formateur f = new formateur();
f.ftIdFormation = formation.fId;
f.ftIdSalarie = (int)unSal.SelectedValue;
db.formateur.Add(f);
}
db.SaveChanges();
return RedirectToAction("Index");
}
return View(formation);
}
笔者认为:
@model MvcAppGestionRH.Models.formation
@using (Html.BeginForm("Create", "Formation", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
@Html.Label("Libelle")
@Html.EditorFor(model => model.fLibelle)
@Html.ValidationMessageFor(model => model.fLibelle)
<label id="fFormateur">Formateur</label>
@Html.ListBox("formateurListe", ViewData["formateurListe"] as SelectListItem[], new {Multiple = "multiple"})
<input type="button" value="+" name="add" />
<select name="select" size="7" >
</select>
<input type="submit" value="Créer" />
}
随着脚本:
$(function() {
$("#Add").click(function() {
$("select").add($('fFormateurListe').selected);
});
});
最好是,如果你能告诉我们你的代码... –