【发布时间】:2014-07-11 08:22:48
【问题描述】:
我开发了一个管理员工编队的应用程序,我将 MVC4 Asp.net 与 Razor 一起使用。
在我的模型中,我有两个班级(我的数据库中的表格)和 formateur(培训师)。
在我的应用程序中,我可以创建一个“编队”,我想添加一个“编队”(培训师)列表,但我不知道我必须做什么。 我认为最好的解决方案是复选框列表,我成功地用 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);
});
});
【问题讨论】:
-
如果您能向我们展示您的代码就更好了...
标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor