ASP.NET MVC - Proper usage of View Model and Command pattern
Seems like the only other decent way go is to use a partial view for rendering the form and use OrderProductCommand
as the view model.
Product.cshtml:
@model ProductViewModel
...
@Html.Partial("Product_OrderForm", new OrderProductCommand { ProductId = Model.Id })
...
Product_OrderForm.cshtml:
@model OrderProductCommand
...
@using (Html.BeginForm("Product", "Home"))
{
@Html.HiddenFor(cmd => cmd.ProductId)
@Html.TextBoxFor(cmd => cmd.Name)
@Html.TextBoxFor(cmd => cmd.Email)
<input type="submit" value="Place order" />
}
...
This way there is no need to create a data map between view models and business objects, and the controller code can be left clean as it was in in Option 1:
[HttpGet]
public ActionResult Product(int id)
{
return View(new ProductViewModel(id));
}
[HttpPost]
public ActionResult Product(OrderProductCommand command)
{
// process command...
}