开发者

Best way to populate ViewData for DropDownLists in Asp.net mvc?

What is the best way to populate ViewData for the dropdownlists which are used in multiple views. If it is done in each and every Action that uses the dropdowns then we are violati开发者_C百科ng the DRY principal. Also ideally we should be caching the regularly used SelectLists (e.g Countries, States etc).


I've done it before with ActionFilters. In my case, I needed a list of sponsors on every page.

http://weblogs.asp.net/psteele/archive/2009/11/04/using-windsor-to-inject-dependencies-into-asp-net-mvc-actionfilters.aspx


You should prepare base view model for these views and enhance it by inheritance with data from specific view:

class BaseViewModel
{
     List<string> Countries;
     List<string> States;
}

In my application I created an object that holds cached dictionaries. It implements IApplicationCache interface (created by me, it returns lists of used dictionaries), which is injected into business logic layer and used to populate view models.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜