Reputation: 1775
I'm developing an ASP.NET web application.
In one view, I have a base model (BaseObject) and depending on a combo box selection there are more fields with specific informations to this object (with own model classes like SpecificObject1, SpecificObject2, ...).
To handle the controller action, my idea is to have the following code:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Update(BaseObject model, FormCollection fc)
{
...
}
My BaseObject
has a property for identifying the specific object, for example: model.SubObjectTypeId
My idea is to have a switch
in the controller action and to "cast" the FormCollection
to the specific model.
Is there a way to do this automatically or do I need to create helper classes which creates an instance of the specific object class and sets the parameters?
The input fields are having the same names as the properties are namend of the specific object.
PS: I'm trying to avoid using AutoMapper. I prefer built-in solutions by ASP.NET MVC.
Thanks for your help.
Upvotes: 0
Views: 1802
Reputation: 5773
Usually in these cases is better to build a ModelBinder a class that has the task to create the correct instance for the controller action. It parses the Form collection and creates the instance to pass to the controller. This way the controllers are thinner and are more respectful of the SRP. :-)
Upvotes: 1