0

Image the following controller method:

public ActionResult ShipmentDetails(Order order)
{
    return View(new OrderViewModel { Order = order });
}

The incoming order parameter is filled from a custom model binder, that either creates a new order for this session and stores it in the session, or reuses an existing order from the current session. This order instace is now used to fill a shipment details form, where users can enter their address and so on.

When using @using(Html.BeginForm()) in the view. I cannot use the same signature for the post method again (because this would result in ambigious method names) and I found me adding a dummy parameter just to make this work.

[HttpPost]
public ActionResult ShipmentDetails(Order order, object dummy)
{
    if (!ModelState.IsValid)
        return RedirectToAction("ShipmentDetails");

   return RedirectToAction("Initialize", order.PaymentProcessorTyped + "Checkout");
}

What are the best practices for this? Would you simply rename the method to something like PostShipmentDetails() and use one of the overloads of BeginForm? Or does the problem originate from the point, that the first method has the order parameter?

1 Answer 1

5

You could use the ActionName attribuite:

[HttpPost]
[ActionName("ShipmentDetails")]
public ActionResult UpdateShipmentDetails(Order order) { ... }

or a more classic pattern:

public ActionResult ShipmentDetails(int orderId)
{
    var order = Repository.GetOrder(orderId);
    return View(new OrderViewModel { Order = order });
}

[HttpPost]
public ActionResult ShipmentDetails(Order order)
{
    if (!ModelState.IsValid)
        return RedirectToAction("ShipmentDetails");

   return RedirectToAction("Initialize", order.PaymentProcessorTyped + "Checkout");
}
Sign up to request clarification or add additional context in comments.

1 Comment

I didn't know about the ActionNameAttribute. Thanks for pointing it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.