I am new to MVC and MVC3 and developing a new application to learn how it works. Mirroring our production environments, it would have several tables with FK relations.
We give the users the ability to maintin the master data.
I have two master tables, Locations and Departments. Since a department can belong to one location, a location FK is put in the department table:
Public Class Location
Public Property LocationID() As Integer
Public Property LocationName() As String
Public Property LocationActive() as Boolean
End Class
Public Class Department
Public Property DepartmentID() As Integer
Public Property LocationID() As Integer
Public Property DepartmentNumber() as Integer
Public Property DepartmentName() as String
Public Property DepartmentActive() As Boolean
Public Overridable Property Location as Location
End Class
When viewing or editing departments, how do I show a list of location names instead of requiring the ID?
You use a combo box that is bound to the LocationId of Department but the corresponding SelectList is populated from the Location table.
You can either pass a list of locations to the view via the model, or use a bespoke Helper method to build the select list.
For display, if you don’t want to use a disabled combo box, write a helper method that takes the Id and returns the LocationName.