views:

175

answers:

3

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", cartItem.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" />
<%} %>
+1  A: 

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")) { %>
Beyers
+2  A: 

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

<% using (Html.BeginForm("SaveOrder", "ControllerName", FormMethod.Post, null))
{ %>
....
Dan Diplo
Bot thanks for the quick response, never new this was possible! I'll try it out and get back to you.
Rob
Ok i tried your solution but it unfortunately doesn't work :( I modified the question with the current situation
Rob
In your example you have called your controller "Controller". This should be the *actual* name of your controller.
Dan Diplo
Was able to solve it otherwise because of changes in the application. But nevertheless thanks for the tip, never new this was possible and could use it very well!
Rob
A: 

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)) { %>
Beyers
Sorry for the confusement but that is what i did in code, but because of company restriction i'm not allowed to post the exact code so i renamed the controller.
Rob