I have a ViewModel with a kinda complex data structure. I need my view to render different partials, based on the type of elements in my viewmodel, kinda like this:
[Validator(typeof(ViewModelValidator))]
class ViewModel {
IEnumerable<Element> Elements;
}
class Element {
string DisplayName;
DisplayType DisplayType; // TextBox, Select, Radiobutton, Checkbox
IEnumerable<ElementValue> Values; // could be a list of values for the dropdown
int Group;
etc...
}
class ElementValue {
...
}
I need this structure to build my view that has dynamic input elements. When i submit the form i was thinking of sending a json representation of my form/values and have a ValueProvider(could be JsonValueProvider?) mapping the values to my ViewModel again. I only need the user added data, so i could just read the data or map it to a new object, but assuming i’m using FluentValidation (http://fluentvalidation.codeplex.com/wikipage?title=mvc) then i’d loose server-side validation right?
So i guess my question would be: should i map to my ViewModel, even though i lack all the information about Elements (besides the values entered) or should i just map to some object i can validate and then find some other way to show the errors client-side?
I dont know if this is the right way but , I have always felt that View Models should not be very complex. When it become complex we may using View Model View pattern but for the sake of using it.
I usually Create View Model very specific to a View but with just enough Properties that I need to “read back” and are required for further processing (making decisions) . This includes hidden fields too. And as Validations are added using DataAnnotations lot of validation and mapping , MVC framework does which saves time.
All data to view only elements like Grid , I send as Json.
Regards,
Mar