我正在尝试在我的MVC应用程序中实现一个checkboxlist。在这里,我需要将选中的checkboxlist值传递给@html.ActionLink()
我搜索了许多站点,包括Stackoverflow,但没有数据库值的模型绑定,并获取选定的复选框值并传递给操作。
Eg :https://stackoverflow.com/questions/4872192/checkboxlist-in-mvc3-0
我试过这个链接。 CheckBoxList multiple selections: how to model bind back and get all selections?
如何从复选框列表中获取所选的productid。 任何例子.. ??
答案 0 :(得分:3)
视图:
@foreach (var item in Model.Tags)
{ <label>
<input type="checkbox" name="tag" value="@item.TagID" @if (item.Selected) { <text>checked="checked"</text> } />@item.Name
</label>
}
控制器:
[HttpPost]
public RedirectToRouteResult Edit(IEnumerable<Guid> tag)
{
using (var dc = new MyDataContext())
{
var existing = dc.Tag
.Select(i => i.TagID)
.ToList();
// remove old
foreach (var id in existing.Except(tags.EmptyIfNull()))
dc.Tag.DeleteOnSubmit(dc.Tag.Single(k => k.TagID == id);
// add new
foreach (var id in tags.EmptyIfNull().Except(existing))
dc.Tag.InsertOnSubmit(new Tag() { TagID = id, });
dc.SubmitChanges();
}
return RedirectToAction("List");
}