Suppose I have a Vehicle class and two child classes like so:
public class Vehicle
{
public string Model { get; set; }
public string Color { get; set; }
}
public class Car : Vehicle
{
public string Seats { get; set; }
}
public class Truck : Vehicle
{
public string MaxLoad { get; set; }
}
My view to create a car or a truck contains a drop down list where you choose car or truck and depending on what you choose the correct form loads into the div with query.
Here is how I implemented this:
- I created a partial view containing a form to create a car with Model, Color and Seats inputs.
- Another partial view with a form to create a Truck with Model, Color and MaxLoad inputs.
Each form submits to its own controller method i.e:
[HttpPost] public ActionResult CreateCar(Car newCar) { .... }
and same for CreateTruck.
- I have a createVehicle view that contains the drop down list, an empty div with an id and some jquery code that loads one of the car or truck partial views into the div.
Now everything works but if i need to change something in my vehicle model, i would then have to change each partial view and each controller method.
So what i'm asking is how do I go about to implement the most optimal way to submit the models in this case?
Optimally I would have:
- One controller method onto which I post the models
- The createVehicle view containing the Vehicle properties and each partial view only containing its own additional properties.
ViewModel
orVM
so it's obvious that they aren't entities. – Chris Pratt