views:

256

answers:

1

I'm trying find a way to do something with MVC that I was able to do with WebForms.

I have a set of steps a user needs to go through to fill in data. In a previous case it was registering animals on an 'classifieds' site that was designed in webforms. For simplicity lets say it has 3 pages. Page 1 asks them what type of animal in a dropdownlist. This would display [dog, cat, sheep, alpaca, horse]. On submitting the page the data would be saved and then transfered to the second page. On the second page they would be required to submit information specific to each animal. Rather than create one large page and hide/show the relevant controls for that animal, or having panel controls to group the data, I chose to create user controls for each animal. This control would inherit from an interface I created called IAnimalTemplate which contained two methods, Load() and Save(). Page two would contain only a placeholder control. I would then take the type of animal and then dynamically load the usercontrol into the placeholder. The magic of the IAnimalTemplate really came into effect when I clicked the submit button to save the data. The submit button was outside of the uer control so I was able to cast the user control to IAnimalTemplate and call the Save method. It worked brilliantly and allowed me to very quickly create numerous templates/user controls for differing animals and store/load the combinations in a database. Because it was the submit button in the page that called a method that then subcalled the Save method on the user control I was then able to continue processing from the page and transfer to page 3.

Now I need to do something very similar with MVC. I've looked into the Html.RenderAction from MVCFutures and have also looked at PartialRequests at http://blog.codeville.net/2008/10/14/partial-requests-in-aspnet-mvc/ but I'm really not sure if these will do the job. These seem to keep the viewmodel within the controls which is ideal because the model data changes between '(animal) templates' but they are going to call their own actions. Of course I can call a Save method which I can define in an interface BUT I'm not sure I can continue executing code outside of the view control (in the page) once this has been executed. Maybe I'm confused on this, I'm hoping someone can shine a light on a solution somewhere. Any ideas or do I need to expand some more?

Thanks Lloyd

A: 

It tends to be bad form to try to solve a problem in the same fashion in a new system. MVC works differently from ASP.NET, so you really should try to solve things the MVC way when using MVC.

There is a similar approach you can take to getting this to work using MVC. You were correct to look into the partial stuff from MVC. In MVC people use partial views. You can put your form controls for the type in the partial view and have it be rendered on the page.

Since MVC does the work of mapping controls onto types when pages submit you will be able to have the controller accept an object of the type you're trying to save. When the save button is clicked it will call the save action which accepts an object of the type you are trying to save and you will be able to save it.

This approach IMO is actually easier than the user control approach you were previously using in ASP.NET Web Forms.

Brendan Enrick
Thanks for the response. I agree with you on your first paragraph, I am trying to find a solution the MVC way rather than trying to port over the previous method. My initial thoughts were to have if statements in a partial view to output based on type, but given the number of types added could end up being pretty large I figured the view, or partial, would end up being pretty smelly and to be honest I'm not sure it'd be the best way to have if/switch statements in the view for this case.
lloydphillips
As for your solution, it makes a little sense but I'm confused on the controller accepting the object. If I have seperate partials for dog, cat, sheep, horse etc am I really going to have my main controller accept object types for that many animals? This is why I was looking at the partial request because I can couple the model to the particular partial and the action to that particular partial. But then this seems to give me issues in processing data outside of my partial (since I'd be calling the partial's action rather than the action on the view page's controller. Hope that makes sense?
lloydphillips
So essentially I'd be calling Animals/Sheep/Update from the view page bu then wanting to run some action from the main controller. Let me know if I'm not making sense.
lloydphillips