Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax.BeginForm inside Html.BeginForm

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.

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?

like image 858
Code Silverback Avatar asked Apr 24 '09 14:04

Code Silverback


People also ask

What is the difference between Ajax BeginForm and HTML BeginForm?

@Html. BeginForm is used to post the data by full page refresh whereas @Ajax. BeginForm performs Post back function and allows some portion of Html to be reloaded rather than overall page refresh.

What is @using HTML BeginForm ()) in MVC?

"BeginForm()" is an extension method for both HtmlHelper and AjaxHelper classes. It returns an MVCForm object from both HtmlHelper and AjaxHelper class instances so there is not much difference but the AjaxHelper method submits the form asynchronously using JavaScript.

What does Ajax BeginForm do?

Ajax. BeginForm is the extension method of the ASP.NET MVC Ajax helper class, which is used to submit form data to the server without whole page postback.

What is HTML BeginForm?

BeginForm is the Html Helper Extension Method that is used for creating and rendering the form in HTML. This method makes your job easier in creating form. Here, is the method to create a form using Html. BeginForm extension method in ASP.NET MVC5. BeginForm("ActionMethod", "ControllerName","Get⁄Post Method")


1 Answers

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.

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.

for instance, updating a quantity span text when you change a dropdownlist (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 Link might help. Regards

Edit:

So.. the link no longer exists. But thanks to the internet wayback machine, we have this copy :)

like image 84
Francisco Avatar answered Oct 07 '22 10:10

Francisco