2010-03-06 41 views
5

我目前正在開發中MVC2 申請我已經想用在我的應用 多發的形式標記在我看來 我創建了具有刪除選項表我正在通過郵政進行個人刪除,所以我已爲每個按鈕創建表單標籤。 我也希望用戶給出選項刪除多條記錄,所以我給他們提供了複選框。這個表單應該具有複選框和所有的值。 這樣的形式得到呈現像這樣我可以有另一種內一種形式標記在ASP.net MVC RC2

對於每一個刪除按鈕

<form action="/Home/MutipleDelete" method="post"> 
<input class="button " name="Compare" type="submit" value="Mutipledelete" style="margin-right:132px;" /> 

<input id="chk1" name="chk1" type="checkbox" value="true" /> 

<form action="/Home/Delete" method="post"> 

    <input type="submit" class="submitLink" value="member1" /> 

    <input type="hidden" name="hfmem1" id="hfmem1" value="1" /> 

       </form> 
<input id="chk2" name="chk2" type="checkbox" value="true" /> 

<form action="/Home/Delete" method="post"> 

    <input type="submit" class="submitLink" value="member2" /> 

    <input type="hidden" name="hfmem2" id="hfmem2" value="2" /> 

       </form> 


      </form> 

下是行不通的。但如果我寫我的代碼的形式呈現這樣

<form action="/Home/MutipleDelete" method="post"> 

<input class="button " name="Compare" type="submit" value="Mutipledelete" style="margin-right:132px;" /> 
</form> 
<input id="chk1" name="chk1" type="checkbox" value="true" /> 

<form action="/Home/Delete" method="post"> 

    <input type="submit" class="submitLink" value="member1" /> 

    <input type="hidden" name="hfmem1" id="hfmem1" value="1" /> 

       </form> 
<input id="chk2" name="chk2" type="checkbox" value="true" /> 

<form action="/Home/Delete" method="post"> 

    <input type="submit" class="submitLink" value="member2" /> 

    <input type="hidden" name="hfmem2" id="hfmem2" value="2" /> 

       </form> 

它是在Mozilla,但不是在IE.I已經調試並檢查值的FormCollection在contoller.What做的工作?

回答

2

從概念上講,表單內的表單實際上並不合理。你不能使用動作鏈接代替內部表單上的按鈕嗎?你仍然可以讓它們看起來像按鈕。

+0

但使用與get方法刪除不是一個好主意也是由斯蒂芬·瓦爾特http://stephenwalther.com/blog/archive/2009/01提/ 21/asp.net-mvc-tip-46-ndash-donrsquot-use-delete-links-because.aspx – coolguy97 2010-03-06 11:26:12

+1

好的,這是一個公平點。在這種情況下,我認爲你需要做出最重要的決定(如果Javascript被關閉,最不可能錯過)功能:單獨刪除或選擇刪除。最重要的一個使用表單,另一個使用Ajax。 – pdr 2010-03-06 12:06:06

5

在XHTML中,表單中的表單是不允許的,它沒有任何意義。

要完成您正在嘗試做的事情,您應該在表格中使用簡單的鏈接/按鈕。例如,這些可以向您的服務器發送「刪除」請求,並在請求成功完成後,使用jQuery從UI中刪除條目。

例子:

[Authorize] 
    [HttpPost] 
    public JsonResult Delete(Guid id) 
    { 
     // do stuff, delete the item 
     return Json(succeeded ? Id.ToString() : "error"); 
    } 
視圖

<a href="#" onclick="$.post('/Stuff/Delete/' + id, function(data) { deleteCallback(data); }); return false;">delete</a> 

function deleteCallback(data) 
{ 
    if(data=="error"){ 
    /* error handler for UI */ 
    } 
    else { 
    /*remove the entry from the user interface*/ 
    } 
}; 

如果你想要做多刪除,你不應該傳遞的ID列表(刪除使用URL,因爲有一個在IE中2k的限制,它看起來討厭),而是使用以下語法:

arrayVariable = new Array(); 
// fill array with ids associated to checked checkboxes' entries 
$.post('/Stuff/Delete/' + id, {'IdsToDelete' : arrayVariable }, function(data) { deleteCallback(data); }); return false;"> 

這可以是自動的盟友序列化爲一個真正的.NET列表,所以你只需要迭代整數或GUID列表,或任何你的PK看起來像!

編輯:這仍然有點簡化。出於安全原因,您應該保護自己免受CSRF(跨網站請求僞造)。達林提供a great answer how to do that

這將[ValidateAntiForgeryToken]添加到您的控制器的方法和觀點都需要包含類似

var token = $('input[name=__RequestVerificationToken]').val(); 
$.post("/YourUrl", { 'IdsToDelete' : arrayVar, '__RequestVerificationToken': token }, function(data) { deleteCallback(data); }); 
+0

感謝其他出路,但我怎麼能實現單一刪除和多個刪除在這種情況下使用後,一個頁面。 – coolguy97 2010-03-06 11:29:37

+0

要使用多重刪除,我會把複選框放在那裏。您可以使用jquery檢查複選框的狀態並傳輸用戶想要刪除的ID列表。然後,單一刪除鏈接保持不變。 我更新了一下這篇文章。 – mnemosyn 2010-03-06 11:40:22

+0

感謝您的回覆,但我不想在那裏使用ajax – coolguy97 2010-03-06 12:06:24

0

爲什麼不直接使用一個單一的形式,並且使用多個提交按鈕?你需要認真解釋結果,但是這可以讓你在沒有javascript的情況下做到這一點,也不需要做扭曲的HTML。

您可能需要直接閱讀Request.Params,但處理起來相對簡單。

<form action="<%= Html.Encode(Url.Action("Delete"))%>"> 
<ul> 
<li>Item 1 <input type="submit" id="<%= IDSafe(item_id) %>" name="<%= IDSafe(item_id2) %>" value="Delete" /></li> 
<li>Item 2 <input type="submit" id="<%= IDSafe(item_id2) %>" name="<%= IDSafe(item_id2) %>" value="Delete" /></li> 
</ul> 
<input type="submit" name="deleteall" value="Delete selected" /> 
</form> 
0

我認爲這將不可能使用後。 自從使用post之後,我們可以發現哪個提交按鈕被點擊了,但如果我們想顯示一些值來顯示我們想要提交給數據庫的用戶和其他值,將無法找到提交按鈕的值。

在這種情況下,我的提交按鈕

是動態生成

<input type="submit" name="submitbutton" id="<%= Html.Encode(item.ideletevalue) %>" class="submitLink" value=" <%= Html.Encode(item.deleteitemname)%>"/>