Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have a model called Car and depending on what type of Car the user select the view is presented differently. For example the user selects from a grid of different cars and depending if it is a Volvo or a Kia or a Ford the view must allow different fields to be editable. For example with a Volvo the color is editable and is mandatory but with a Kia it is not.

I would like to use the one Car class to bind the view but want the client side validation to pick up the required fields based on what type of car.

I want to go only to one Action method for the Update

what is a good way to approach this problem...? create a base class and inherit from it? will this give me binding problems..?

share|improve this question
add comment

6 Answers

The state of the fields are the domain of your model. The view should have absolutely no "smarts" within it in determining the enabled or disabled state of its widgets. All "logic" must be provided by the model itself. Binding should be done polymorphically within the model, not the view.

The view must be able to request this information in such a way that a polymorphic response is returned by the model.

for example, this method is fired by the Model when its state has changed. It provides its Observers information about the selected Car in a polymorphic manner ...

Class View {

// view registers itself as a listener of model changes ...
Model.addModelStateListener(this);

// model has changed ... 
// Car instance can be a KIA, Ford or Volvo. Car is simply a common interface 
// and each instance defines its state differently

public void modelChanged(Car instance){
    ColorWidget.value = instance.getColorState();
}

}

share|improve this answer
    
Another way to implements this that avoids either the model or the view having any smarts about these differences is to have View subclasses that are used for each car type that are simply ard-coded to act in the desired way. In some circumstances, this can be actually less brittle than trying to write a lot of logic around state in either the model or the View--the only decision to be made is which View to use, which is relatively simple. –  Amy Blankenship Nov 3 '13 at 13:26
add comment

To ensure the view provides the right capabilities, you should at the least extend the Car class with (a) method(s) that allow the view to get to know if a particular property is mandatory/editable/present.

Inside the model, this information can either be generated dynamically based on the values of various properties, or you can use subclasses of Car to encode the various possibilities. This choice should mostly be based on how fundamental the differences between a Ford and a Volvo are for your model. You should not create different classes just to differentiate in the editability of some properties.

Also, if you go for the subclass approach, each sub-class should be aware of all the properties that all the other subclasses support, at least in so far as to be able to gracefully handle attempts to access/modify non-existing or non-editable properties.

share|improve this answer
add comment

If you are speaking in terms of asp.net's mvc, then you may want to create a custom attribute and put all the logic inside that class and add the name of the class as an attribute to a property inside your car class.

This link may help you: how to create a custom attribute

share|improve this answer
add comment

If all the cars have the same properties and behaviour then I would have a single car class and make it implement IValidatableObject interface. In the 'Validate' method look at the type of the car and add an error accordingly.

With regards to client side validation, you will have to implement a custom validator in jQuery or library of your choice. I personally use KnockoutJS along with KnockoutJS Validation plugin. Simple example can be found here: http://stackoverflow.com/questions/241145/jquery-validate-plugin-how-to-create-a-simple-custom-rule

share|improve this answer
add comment

Here's one way to design this feature.

Classes

public class Car {}

public class Ford : Car {}

public class BMW : Car {}

Controller

public ActionResult Index()
{
    var car = new BMW();

    return View(car);
}

The above is just test code. Now the key part is this. In the Views\Shared folder, create a new folder named EditorTemplates. In that new folder, create two partial views:

Ford.cshtml

@model CarViews.Controllers.Ford

<p>This is a Ford</p>

bwm.cshtml

@model CarViews.Controllers.BMW

<p>This is a Bmw</p>

Then in the Index view you can do this:

@model CarViews.Controllers.Car

@Html.EditorFor(m => m)

Depending on what type of car you pass in, the editor view specific to that type will be rendered for you. As you add new cars to the system, you simply add new cshtml files for those cars.

share|improve this answer
add comment

You could add one or more Dictionary<string,bool> properties to the car class and in the constructor, set a list of all fields and whether or not they're enabled. When the view loads, iterate through the dictionary and enable or disable input fields or validation messages accordingly.

If its in .NET MVC, there's always the [Required()] data annotation. Which makes it very simple to check if the Model State is/isn't valid.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.