I have a scenario that is giving me a headache, and I thought I was going in the right direction, now I have found another problem. It's complicated so, Let's start at the database:
I have a table with a key, and a value, a typeId and a few other properties. All I'm concerned with are the key and value essentially. My viewmodel contains a list of the Domain Object I mapped from the Data Object nhibernate uses in the repository layer. Here it is:
public class DomainModel
{
public string Key { get; set; }
public string Value { get; set; }
public TypeEnum Type { get; set; }
}
**Note, the TypeEnum has nothing to do with the strong type of the Value for the Key. It is just a different way of categorizing the Keys/Values so that I can pull them from the database by that type.
Simple. Here is my viewmodel:
public class ViewModel
{
public List<DomainModel> Models { get; set; }
}
The problem I have is that the values for the keys are different data types. Sometimes they are booleans and I want an Html.CheckBoxFor(model => model.Value), and sometimes they are strings and an Html.TextBoxFor(model => model.Value) will suffice.
Here is my razor view:
@foreach (var setting in Model.Models)
{
<tr>
<td>@Html.Label(setting.Key)</td>
<td>@Html.TextBox(setting.Value)</td>
</tr>
}
What is the best area of the application to slice here, and do some type inspection or something so that I have the appropriate Html Elements on the page? How would I even go about that? Am I missing something that is really obvious and simple here? Additionally, how do I go about getting a Display Name Attribute for the Keys, based on the Key's value? They are just PacalCasedBunchedDescriptionNames at the moment. Am I just way off on design here or what?