Я пытаюсь следующим образом,
Модель: Product.cs
public class Product
{
public string ID { get; set; }
public string Name { get; set; }
public string Brand { get; set; }
public string Price { get; set; }
}
Контроллер: HomeController.cs
[HttpPost]
public ActionResult SaveProduct(Product obj)
{
ViewBag.Message = "Product saved successfully!";
return View("Index", obj);
}
[HttpPost]
public ActionResult CancelProduct(Product obj)
{
ViewBag.Message = "The operation was cancelled!";
return View("Index", obj);
}
Просмотр: index.cs html
@model ViewBag_array.Models.Product
<h1>@ViewBag.Message</h1>
@Html.DisplayForModel()
@using (Html.BeginForm("", "Home"))
{
@Html.EditorForModel()
<br />
<input type="submit" name="save" value="Save" formaction="SaveProduct" formmethod="post" />
<input type="submit" name="cancel" value="Cancel" formaction="CancelProduct" formmethod="post" />
}
Таким образом вы получаете четкое разделение проблем, и код становится более читабельным.