Should an object be created in an Unhealthy State? Passing from Create controller to View
What are the advantages of passing a blank object eg Client from the controller?
public ActionResult Create()
{
Client client = new Client();
return View(client);
开发者_如何学Go }
//
// POST: /Client/Create
[HttpPost]
public ActionResult Create(Client clientToAdd)
{
try
{
clientRepository.Insert(clientToAdd);
return RedirectToAction("Index");
As opposed to:
public ActionResult Create()
{
return View();
}
//
// POST: /Client/Create
[HttpPost]
public ActionResult Create(Client clientToAdd)
{
try
{
clientRepository.Insert(clientToAdd);
return RedirectToAction("Index");
The reason being is that: Should an object (eg the Client) be created in an 'unhealthy' state ie blank?
Cheers
Dave
Your first technique is actually the preferred method considering MVC2's templated helpers and EditorFor and DisplayFor enhancements. You must have a model object in order for metadata to be derived from it. No model, no metadata, an important feature of convention over configuration driven MVC architecture.
This technique also provides convenient defaults to be provided and like the other answerer has stated, allows you to reuse add/edit views for simple scenarios.
精彩评论