views:

35

answers:

1

Hi

What are the advantages of passing a blank object eg Client from the controller?

public ActionResult Create()
        {
            Client client = new Client();
            return View(client);
        }

        //
        // 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

A: 

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.

jfar