Monday, 9 September 2013

MVC - should a Model return a List instead of itself or should a new model be created

MVC - should a Model return a List instead of itself or should a new model
be created

I am using MVC and part of the project is a product selector. The user
answers some questions and then after answering these questions 3
suggested products are returned.
The model is:
public class TVDBContext : DbContext { public int Width { get; set; }
public int Height { get; set; } public int Length { get; set; } public
string Colour { get; set; } public string Type { get; set; }
public DbSet<TV> TVs { get; set; }
}
The first question the user answers is how much space they have for the
TV. Depending on the answer certain types of TVs are excluded as none of
the TVs of that type can fit in the space they have. This then means that
certain options are excluded from being asked on the second question which
asks what type of TV they would like. I want to make an AJAX call to the
TV controller which then calls the TV model to find out from the database
what TV types should be excluded from the type question. I am wondering
whether I should have a method in the TV model to return to the controller
what TV types should be excluded. Some thing like this:
public List<string> GetAllowableTypes(int width, int height, int
length)
{
//access database to get the TVs that can fit in the space
//use LINQ to select the distinct TV Types
//return a list of the types of TVs that will fit
}
Or should I create a separate model that queries the TV table?

No comments:

Post a Comment