gpt4 book ai didi

asp.net-mvc-2 - 我可以在 ASP.net MVC RC2 中的另一个里面有一个表单标签吗

转载 作者:行者123 更新时间:2023-12-04 17:13:39 25 4
gpt4 key购买 nike

我目前正在 MVC2 中开发应用程序
我想在我的应用程序中使用多个表单标签
在我看来
我创建了一个表,其中包含我正在通过 Post for Individual Delete 执行的 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>

以下是行不通的。但如果我写我的代码,表单以这种方式呈现
    <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 中无效。我在 formcollection 中调试和检查值在 Controller 中。该怎么办。?

最佳答案

在 XHTML 中,表单中的表单是不允许的,它没有任何意义。

为了完成您想要做的事情,您应该在表格中使用简单的链接/按钮。例如,这些可以向您的服务器发送“删除”请求,并且在请求成功完成后,还使用 ​​jQuery 从 UI 中删除条目。

例子:

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

在 View 中
<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*/
}
};

如果要进行多次删除,则不应使用 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;">

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

编辑:这仍然有点简化。出于安全原因,您应该保护自己免受 CSRF(跨站点请求伪造)的侵害。达林提供 a great answer how to do that .

这将添加 [ValidateAntiForgeryToken]到您的 Controller 方法, View 将需要包含类似
var token = $('input[name=__RequestVerificationToken]').val();
$.post("/YourUrl", { 'IdsToDelete' : arrayVar, '__RequestVerificationToken': token }, function(data) { deleteCallback(data); });

关于asp.net-mvc-2 - 我可以在 ASP.net MVC RC2 中的另一个里面有一个表单标签吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2392238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com