C# Ajax.BeginForm 里面的 Html.BeginForm
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/786055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ajax.BeginForm inside Html.BeginForm
提问by Code Silverback
I have a view that is used for editing stuff, say Orders. Orders have line items that can be added arbitrarily. So a main view and nested partialviews.
我有一个用于编辑内容的视图,比如订单。订单具有可以任意添加的行项目。所以有一个主视图和嵌套的局部视图。
Each partial should have an ajax form for tweaking quantities of each line item or whatever.
每个部分都应该有一个 ajax 表单,用于调整每个行项目的数量或其他任何内容。
Thus:
因此:
Html.BeginForm()
{%>
Ship to: blah blah blah
<%
Ajax.BeginForm("EditLineItem", "Order", new { OrderLineItemID = Model.ObjectID }, itemAjaxOptions))
{
Item qty blah blah blah
<--! (ajax form's submit button, etc.)-->
}
%>
<--! (ajax form's submit button, etc.)-->
<%
}
I have a controller that looks like this:
我有一个看起来像这样的控制器:
[ActionName("Edit")]
[AcceptVerbs(HttpVerbs.Post)]
[ValidateAntiForgeryToken]
public ActionResult Edit(int orderID)
{
blah, blah
}
[ActionName("EditLineItem")]
[AcceptVerbs(HttpVerbs.Post)]
[ValidateAntiForgeryToken]
public ActionResult EditLineItem(Guid orderLineItemID)
{
blah, blah
}
My trouble is that when I submit the Ajax form, I get the Edit method instead of the EditLineItem methods. Both routes are mapped. Is there some gotcha like "you can't submit an Ajax form inside of an Html form" that I don't know about?
我的问题是,当我提交 Ajax 表单时,我得到的是 Edit 方而不是 EditLineItem 方。两条路线都被映射。是否有一些我不知道的问题,例如“您不能在 Html 表单中提交 Ajax 表单”?
回答by Francisco
I tried the exact same thing a while ago. No matter what I did, it wouldn't do the AJAX submit. So I think the answer is: yes, you can't put a submit button for an AJAX form inside a regular html form.
不久前我尝试了完全相同的事情。无论我做什么,它都不会执行 AJAX 提交。所以我认为答案是:是的,您不能在常规 html 表单中放置 AJAX 表单的提交按钮。
But, why would you have partial submits merged with full submits? The easiest workaround to this imo would be to use JSON requests with jQuery.
但是,为什么要将部分提交与完整提交合并?对此 imo 的最简单解决方是将 JSON 请求与 jQuery 一起使用。
for instance, updating a quantity span text when you change a dropdownlist (id=Order):
例如,当您更改下拉列表 (id=Order) 时更新数量跨度文本:
<script type="text/javascript">
$(document).ready(function() {
$('select#Order').change(function() {
$.getJSON('/Orders/UpdateQty/' + this.value, {},
function(data) {
$('#qty').html(data);
});
});
});
</script>
And the code in the "Orders" controller:
以及“订单”控制器中的代码:
public class OrdersController : Controller
{
public ActionResult UpdateQty(int id)
{
return Json(yourLibrary.getQuantities(id));
}
}
This Linkmight help. Regards
此链接可能会有所帮助。问候
Edit:
编辑:
So.. the link no longer exists. But thanks to the internet wayback machine, we have this copy:)
所以..链接不再存在。但是多亏了互联网回归机器,我们有了这个副本:)
回答by user146282
AFAIA the AjaxForm is still renders as a form tag, so you'd be nesting forms, which as you've found is a no no.
AFAIA,AjaxForm 仍然呈现为表单标签,因此您将嵌套表单,正如您发现的那样,这是不可以的。
I reckon Francisco is on the right lines (tho I'd suggest implementing a post rather than a get as you are updating something).
我认为弗朗西斯科是在正确的路线上(虽然我建议在更新某些内容时实施帖子而不是获取)。
Kind regards, TP
亲切的问候,TP