Reputation: 489
I have a bunch of tables. Each table has a "Status" column. The column has either the character A or I. A= Active and I= Inactive.
I have a create view corresponding to each table. I want to display a dropdownbox that shows two values- Active and Inactive and then map them accordingly.
I know I can do following in each view where I need the dropdown for status
@Html.DropDownListFor(model => model.status, new SelectList(new[] { new { ID = "A", Desc = "Active" }, new { ID = "I", Desc = "Inactive" } }, "ID", "Desc"))
however if tomorrow I decide to have add one more status i will have to change each and every view.
The other option is to create a dictionary and pass it through a view model sort of as explained in this article
however that means I have to create a viewmodel for each of my models just to accomodate the statuslist.
is there any other way I can achieve this?
Upvotes: 0
Views: 467
Reputation: 11
Following link might be helpful for binding the dropdown list in mvc
http://www.c-sharpcorner.com/UploadFile/deveshomar/ways-to-bind-dropdown-list-in-Asp-Net-mvc/
Upvotes: 0
Reputation: 844
I recommend using a View Model for each unique View. Long term it makes the UI easier to work with and maintain.
The View Model approach can be taken to an extreme and become counterproductive in my experience. For example, when creating a VM for a Customer Entity, I do not recreate a CustomerVM that has all the same properties of the Customer Entity. Instead, I just create a Customer Property on the CustomerVM that holds the entire Customer Entity. Some may disagree with this approach because I may be exposing more info to the View than it needs, if the view is not displaying all of the Customer Entity info. It's true, but I like solutions that are fast and easy to implement and maintain.
You never know what is going to be necessary in the future. Over time I have found this approach to be the most flexible solution.
So, then you could create a Base View Model for all views that have common look-up lists and have your new View Models inherit from this Base VM.
It's one way to do it. :)
Upvotes: 1