我可以有另一种内一种形式标记在ASP.net MVC RC2 [英] Can i have One form tag inside Another in ASP.net MVC RC2
问题描述
我目前正在开发的应用程序MVC2
我曾经想用多发表单标签我的应用程序
在我看来
我创建了具有删除对此我通过邮政做选项个人删除,所以我必须为每个按钮创建表单标签表。
我也希望用户给选项删除多发的记录,所以我为他们提供checkboxes.This形式应该有复选框,所有的所有值。
这样的形式得到呈现像这样
I am currently Developing application in MVC2 I have want to used mutiple form tags in My application In My View I have Created a table which has Delete option which i am doing through Post for Individual Delete so i have Create form tag for each button. i also want user to give option to delete mutiple records so i am providing them with checkboxes.This form should have all the values of checkboxes and all. so form gets render Like this
对于每一个删除按钮
<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>
以下是行不通的。但如果我写我的code的形式呈现在这样
The following is not working . but IF i write my code that form renders in this way
<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调试和经过值做?
it is working in Mozilla but not in IE.I have debug and Checked values in formcollection In contoller.What to do.?
推荐答案
在XHTML中,表单中的一个形式是不允许的,它并没有真正意义。
In XHTML, a form in a form is not allowed and it doesn't really make sense.
要完成什么你正在尝试做的,你应该在你的表使用简单的链接/按钮。这些可能,例如,发送删除的请求到服务器,并请求成功的完成时,也删除使用jQuery UI中的条目。
To accomplish what you are trying to do, you should use simple links/buttons in your table. These could, for example, send a 'delete' request to your server and, upon succesful completion of the request, also remove the entry from the UI using jQuery.
例如:
[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的限制,它看起来讨厌),而是使用以下语法:
If you want to do multi-delete, you should not transfer the list of IDs to delete using the URL (because there is a 2k limitation in IE and it looks nasty), but instead use the following syntax:
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样子!列表
This can be automatically serialized to a real .NET list, so you will just have to iterate list of integers or GUIDs, or whatever your PKs look like!
编辑:这仍然是一个有点简化。出于安全原因,你应该保护自己免受CSRF(跨站请求伪造)。达林提供一个伟大的答案如何做到这一点。
This is still a bit simplified. For security reasons, you should protect yourself from CSRF (Cross-Site Request Forgery). Darin provided a great answer how to do that.
这将添加 [ValidateAntiForgeryToken]
到控制器的方法和观点都需要包含类似
This will add the [ValidateAntiForgeryToken]
to your controller's method and the view will need to contain something like
var token = $('input[name=__RequestVerificationToken]').val();
$.post("/YourUrl", { 'IdsToDelete' : arrayVar, '__RequestVerificationToken': token }, function(data) { deleteCallback(data); });
这篇关于我可以有另一种内一种形式标记在ASP.net MVC RC2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!