开发者

MVC: Submit one form from a page with multiple forms

I'm working in an ASP.NET 3.5 MVC application where i've got a view with multiple forms in it. Form 1 contains information about products and will form a shoppingcart with a remove button. Form 2 is as below and is to be used to get the information from the shoppingcart and create an order of it.

<% using (Html.BeginForm()) { %>
   <%=Html.Hidden(Order.ProductId", "E63EF586-F625-4C8D-B82E-E63A6FA4A63C")%>
   <%=Html.Hidden(Order.Amount", 16)%>
   <input type="submit" value="Pay" />

The first form contains a similar beginform with a foreach loop to get the information about the products from the model. When i use the submit button on the second form everything seems to be submitted and the action in the controller for the first form is trying to handle the request. That is where the error occurce because the information in the viewmodel isn't corresponding to what is being expected.

The controller which is trying to handle the request:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ShowShoppingcartForm([Bind(Prefix = "Order")] MyViewmodel viewModel)
{
//..
}

The controller which is expected to handle the request:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveOrder([Bind(Prefix = "Order")] MyViewmodel viewModel)
{
    //..
}

AFTER MODIFICATIONS:

Ok, i tried your suggestion but unfortunately i can't get it to work. I changed things as below;

Form 1:

<%foreach (var cartItem in Model.ProductList) { %>
  <% using (Html.BeginForm("ShoppingCart","ControllerName", FormMethod.Post,null)) { %>
        <tr>     
            <td> <%=Html.Hidden("Cart.ProductVariantId", cartItem.ProductVariant.Id)%>
                 <%=Html.Encode(cartItem.ProductVariant.Product.Name)%> </td>
            <td> <%=Html.Encode(cartItem.ProductVariant.Product.Description)%> </td>
            <td> <%=Html.TextBox("Cart.Amount", cartIt开发者_C百科em.Amount)%></td>
            <td> <%=Html.Encode(cartItem.Amount)%> </td>
            <td> <%=Html.Encode(cartItem.Product.PriceInCents)%> </td>
            <td> <input type="submit" value="Remove" width="50px" /> </td>
            <td> <input type="submit" value="Submit" width="50px" /> </td>
        </tr>
        <% } %> 
    <% } %>

Form 2:

<% using (Html.BeginForm("SaveOrder", "ControllerName", FormMethod.Post, null))
{%>

   <%=Html.Hidden("Order.ProductId", "E63EF586-F625-4C8D-B82E-E63A6FA4A63C"")%>
   <%=Html.Hidden(Order.Amount", 1)%>
   <input type="submit" value="Pay" />
<%} %>


You can optionally specify the name of the action and controller with Html.BeginForm

<% using (Html.BeginForm("SaveOrder", "ControllerName", FormMethod.Post, null))
{ %>
....


You should use the overloaded form of Html.BeginForm on the second form to specify your action and controller to use, e.g:

<% using (Html.BeginForm("SaveOrder", "YourController")) { %>


Rob,

You should specify the NAME of your controller and not just "Controller" as per your edited code. So if your SaveOrder action is in the HomeController controller class then you should use:

<% using (Html.BeginForm("SaveOrder", "Home", FormMethod.Post, null)) { %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜