好的,我使用的是Select2。我做"创造方法"使用select2,它的工作很棒,好吧,我该怎么做"编辑方法" ??在EditMethod中,我想在List中看到已选择的值并添加新的,如果我想要的话。 这里的代码对于createMethod,我真的很新,请帮忙!
public ActionResult Edit(int id = 0)
{
NIRs nir = db.NIRs.Find(id);
if (nir == null)
{
return HttpNotFound();
}
ViewBag.IFile = new MultiSelectList(db.IFile, "Id", "NazvanieFaila");
return View(nir);
}
[HttpPost]
public ActionResult Edit(NIRs nir, int[] selectedFiles)
{
NIRs newNIR = db.NIRs.Find(nir.Id);
newNIR.Name = nir.Name;
if (selectedFiles != null)
{
foreach (var FileId in selectedFiles)
{
IFile files = db.IFile.Find(FileId);
nir.IFile.Add(files);
}
}
else
{
nir.IFile.Clear();
}
db.Entry(newNIR).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("NirList");
}
和ViewCode:
@using NIR.Models
@model NIR.Models.NIRs
<link href="~/Content/select2.css" rel="stylesheet" />
<script src="~/Scripts/select2.js"></script>
<div align="center">
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<h2>Edit</h2>
<legend>
<div style="overflow-x: auto; width: auto;">
<table align="center" class="TFtable">
<tr>
<td>
<div class="editor-label">
Name:
</div>
</td>
<td>
<div class="editor-field">
@Html.TextAreaFor(model => model.Name)
</div>
</td>
</tr>
<tr>
<td>
<div class="editor-label">
Files:
</div>
</td>
<td>
<div class="editor-field">
@Html.ListBox("IFile")
</div>
</td>
</tr>
</table>
</div>
</legend>
<p>
<input type="submit" value="Save Changes" />
</p>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
</div>
<script type="text/javascript">
$("#IFile").select2(
{
placeholder: "Choose files",
width: 300
});
</script>