I have two classes Item and Category. Items may belong to a single category. In the implementation I am using NHibernate and Item has a Category property (of type Category). Both classes are entities.
I wonder how to make it possible in a view for editing an item to be able to choose a category for example from a list or drop-down list. There are HTML helpers like Html.DropDownListFor but I don’t know how to make it work when I need to select an object (NHibernate doesn’t make CategoryId for Item accessible). Could anybody help me with the problem?
Thanks in advance
Lukasz
You should be using view models in all cases. So no matter how your NHibernate models look like you are trying to display a drop down list in the view. So as always you start by defining a view model that will hold the necessary information to be used by this view:
and then you would have a controller action which will fetch the model from a repository and map it to this view model:
and in the view simply:
If you don’t use view models you will be struggling to adapt your existing models to situations for which they weren’t meant to be.