views:

163

answers:

1

hi there. have looked at Phil Haacks project on books at

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

which has been useful, but I have a mix of data types.

I use a modelview so that i can have a mix of objects, in this case: Order (ie order.id, order.date etc), Customer, SoilSamplingOrder and a list of SoilSamplingSubJobs which is like this [0].id, [0].field, [1].id, [1].field etc Perhaps I should be using ICollection instead of List? I had problems getting UpdateModel to work so I used an extract from collection method. the first 4 method calls : orderRepository.FindOrder(id); etc give the model the original to be edited. but after this point i'm a little lost in how to update the subjobs. I hope i have delineated enough to make sense of the problem.

 [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {

            Order order = orderRepository.FindOrder(id);
            Customer cust = orderRepository.FindCustomer(order.customer_id);
            IList<SoilSamplingSubJob> sssj = orderRepository.FindSubOrders(id); 
            SoilSamplingOrder sso = orderRepository.FindSoilSampleOrder(id);

            try
            {

                UpdateModel(order, collection.ToValueProvider());

                UpdateModel(cust, collection.ToValueProvider());

                UpdateModel(sso, collection.ToValueProvider());



                IList<SoilSamplingSubJob> sssjs = orderRepository.extractSSSJ(collection);

                foreach (var sj in sssjs)
                    UpdateModel(sso, collection.ToValueProvider());


                orderRepository.Save();

                return RedirectToAction("Details", new { id=order.order_id});

            }
            catch
            {
                return View();
            }
        }
+1  A: 

I think you should work on developing a view model that reflects the data that you need to get back and create display/edit templates for that model that renders the view model using Phil Haack's methods for your lists of objects -- in this case, arrays of submodel classes. Let the model binding framework build the returned model (as a parameter) to your action, then reconstitute your domain models from the view model data. Brad Wilson has an excellent series of articles on templating that should be helpful.

tvanfosson
Thanks tvanfosson!
bergin
checked it out but its a little over my head! just trying to update a series of subjobs
bergin
@bergin - I can speak from practical experience. If you invest the time to understand how to use view model and templates, your code will be easier, smaller, and much more robust. When I started using MVC I was doing things much like you, but I've found that nearly without exception using models has increased my productivity and made my code much simpler.
tvanfosson
@tvanfosson yes, starting to get my head round it all. in the end i solve it by adding the object (int id, FormCollection collection, Subjob sssj) and working with it that way; and yes its easier
bergin