I have two models; one holding the values of the fields that are displayed on the page and the other holding the required fields for that page depending on a user variable.(think of it as a combo box that for on each variable, different fields are rendered for the page). My question is what is the most efficient and manageable way to use those two models in a single view/controller? I tried to tuple them but I get that I need to create a parameter-less constructor error. Thank you.
Asked
Active
Viewed 702 times
0
-
1you can use a viewmodel which consists of fields of both models you need – tarzanbappa Oct 29 '14 at 09:25
-
Thank you tarzanbappa for your fast response. The thing is though that the second model(the one that marks the fields as required for the page or not) is a new requirement of the system and implementing this method will require us to recreate all of our system's view models. Is there another way around it? thank you. – CodeMagician Oct 29 '14 at 09:53
2 Answers
0
The best approach would be to have a view model designed specifically for the UI which, by the sounds of things, would be a hybrid of your two current models.
You can use DataAnnotations to add metadata to each property and then Validate your model e.g.
public class ViewModel
{
[Required]
public string PropertyOne { get; set; }
[Required]
public string PropertyTwo { get; set; }
}
...
var model = new ViewModel();
var results = new List<ValidationResult>();
if (!Validator.TryValidateObject(model, new ValidationContext(model), results)) {
Console.WriteLine("Model is not valid:");
foreach (var r in results) {
Console.WriteLine(r.ErrorMessage);
}
} else {
Console.WriteLine("Model is valid");
}

James
- 80,725
- 18
- 167
- 237
-
Hello James and thank you for your response. I think i am either missing something or i was not clear in my description of the problem. Which fields are required will be dynamically decided upon depending on the user choice. For example if the user chooses 2-wheeled the attribute motorbike-name will be required while the attribute car-name will not. If the user chooses 4-wheeled car-name will be required and motorbike-name will not. Sorry if that was not clear at the description... – CodeMagician Oct 29 '14 at 10:27
-
@CodeMagician sounds like you would need a custom attribute to make that dependency - see [this answer](http://stackoverflow.com/a/15975880/82586) – James Oct 29 '14 at 12:29
0
As tarzanbappa said, one of the best approaches would be for you to add the extra fields in each ViewModel.
In order for you to not go into each ViewModel and add all the properties you need, you can add a variable instance of your other ViewModel.
For example: (Your ViewModels)
public class MyViewModel()
{
public T PropertyOne { get; set; }
public T PropertyTwo { get; set; }
public MyOtherViewModel PropertyThree { get; set; }
}
public class MyOtherViewModel()
{
public T PropertyOneCheck { get; set; }
public T PropertyTwoCheck { get; set; }
}
Then in your DataModel call, set your variable. For Example: (LINQ)
...
PropertyThree = (from r in context.Table
where conditionIsMet
select new MyOtherViewModel
{
PropertyOneCheck = r.PropertyOneCheck,
PropertyTwoCheck = r.PropertyTwoCheck
}).FirstOrDefault();
...
And in your View You can toggle the visibility of fields as follows:
@if(Model.PropertyThree.PropertyOneCheck)
{
//Show Field
}else
{
//Hide Field
}

Giorgos Neokleous
- 18
- 2