我可以在ASP.net MVC RC2中的另一个内部使用一个表单标记

coo*_*y97 5 asp.net-mvc-2

我目前在MVC2中开发应用程序我想在我的应用程序中使用多个表单标签在我的视图中我创建了一个具有删除选项的表,我正在通过Post for Individual Delete进行,所以我为每个按钮创建了表单标签.我还希望用户提供删除多个记录的选项,以便我为他们提供复选框.此表单应具有复选框和所有复选框的所有值.所以表格得到渲染像这样

对于每个删除按钮

 <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>
Run Code Online (Sandbox Code Playgroud)

以下不起作用.但如果我以这种方式编写形成渲染的代码

    <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>
Run Code Online (Sandbox Code Playgroud)

它在Mozilla中工作,但在IE中没有.我在formcollection中有调试和Checked值在contoller中.要做什么.

mne*_*syn 5

在XHTML中,不允许表单中的表单,它实际上没有意义.

要完成您要执行的操作,您应该在表中使用简单的链接/按钮.例如,这些可以向您的服务器发送"删除"请求,并且在成功完成请求后,还可以使用jQuery从UI中删除该条目.

例:

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

在视图中

<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*/
  }
};
Run Code Online (Sandbox Code Playgroud)

如果你想进行多次删除,你不应该使用URL传输要删除的ID列表(因为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;">
Run Code Online (Sandbox Code Playgroud)

这可以自动序列化为真正的.NET列表,因此您只需迭代整数或GUID列表,或者您的PK看起来如何!

编辑:这仍然有点简化.出于安全考虑,您应该保护自己免受CSRF(跨站点请求伪造)的侵害.达林提供了一个很好的答案,如何做到这一点.

这将添加[ValidateAntiForgeryToken]到您的控制器的方法,视图将需要包含类似的东西

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


Dar*_*rov 4

不允许嵌套表单,这可能会导致浏览器之间出现未定义的行为。