I’m building a site using mvc2 and have a structure like follows:
Factory -> Domain.objects -> mvc2
One factory contains an enum for logType, which details the actions which have been applied to an object.
The domain requests the object and supplies it to a MVC2 model, this is passed to a view as IEnumerable Ilog, and the view iterates over it.
My issue is that I want the view to create a link based on the log type, so I have a switch statement in the view which creates this functionality. The switch statement is utilising the enum right back in the repository, but this seems a bit wrong to me: exposing the repository to the view.
Should I encapsulate the Log type in a new IList of objects in the domain? Or is it ok referencing this enum in the repository.
If neither of these are ideal, what is the best solution?
Thanks for any help I might get.
The best solution would be to use a view model. A view model is a class which is specially tailored to the needs of the view. So here’s a typical workflow for a controller action:
So in this scenario the view has no knowledge of any of your domain models. That’s in general.
Now for your particular case of generating different links based on the value of the enum I think that writing ifs and switches in a view makes in ugly. Don’t you find? So writing a custom HTML helper that will generate the proper link based on the view model would be great especially when your view looks like this: