考慮以下幾點:如何用一個鏈接刪除MVC中的多個項目?
管窺:
<%= Html.ListBox("BlackList", Model.Select(
x => new SelectListItem
{
Text = x.Word,
Value = x.ID.ToString(),
Selected = Model.Any(y=> y.ID == x.ID)
}))%>
主視圖:
<td><% Html.RenderPartial("GetBlackList", ViewData["BlackList"]); %></td>
控制器:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DeleteWord(int[] wordIdsToDelete)
{
if (!ModelState.IsValid)
return View();
try
{
_wordManager.DeleteWord(wordIdsToDelete);
return RedirectToAction("WordList");
}
catch
{
return View();
}
}
模型(WordManager)
public void DeleteWord(int[] idsToDelete)
{
var dataContext = GetLinqContext();
var currentList = GetTabooWordList();
foreach (var id in idsToDelete)
{
foreach (var item in currentList)
{
if (item.ID == id)
{
dataContext.BadWords.DeleteOnSubmit(item);
}
}
}
dataContext.SubmitChanges();
}
問題是如何正確傳遞參數 - idsForDel? I.E我必須將客戶端數據傳遞給服務器?
<%= Html.ActionLink("Delete Selected", "DeleteWord", "AdminWord", new { wordIds = idsForDel })%>
我認爲這可以通過jQuery。有任何想法嗎?
我有你的主意,謝謝。但是如果我想傳遞一個數組呢? – Alexander 2009-10-05 05:47:36
我錯了。 dataContext.BadWords.DeleteAllOnSubmit( currentList.Where(item => idsToDelete.Containts(item.id))。ToList() ); – takepara 2009-10-05 14:51:48