tags:

views:

23

answers:

1

I am very new to MVC and LINQ.And have some questions.

I have a table named users where I am storing "M" or "F" for male and female.There is another table called Genders Which is linked to the Users table.I have Users Model in ASP.NET MVC application.My question is when I show data where should I create a property to convert that "M" to "Male" and "F" to "Female".I already have UsersFormViewModel . Should I add it there or directly to UsersModel.

Thanks a lot

+1  A: 

My rule for MVVM (which is what you sound like you're doing as you have a UsersFormViewModel class) is to avoid performing view-like activities in the Model.

The Model can either replicate the data directly from the database (anemic model) or it can be a business object (domain model). In either case the model should only contain data that is necessary and sufficient to represent itself.

The ViewModel should contain everything that the View will need to adapt the Model to be displayed. Right down to things like properties for "SaveActionEnabled", etc, so that UI behaviour is controlled by data binding.

In your case, you store "M" & "F" in the database so it is appropriate for the Model to ensure that only these two values are used, so you might want to consider creating an enum for this purpose.

public enum Gender
{
    Male,
    Female,
}

Then have this property in your model:

public Gender Gender { get; set; }

Your ViewModel should then map the enum to a string. This is easy using ToString().

Now you have a consistent representation of your two gender states from the database and you can display the data to the user.

How does that work for you?

Enigmativity