开发者

Displaying data objects mapped by ID elegantly in views

I have a bunch of support Cases and Users. E开发者_如何学运维ach Case is assigned to a User with a unique ID. My data model represents this with an AssignedUser user ID reference. This is easy to store and retrieve, but not convenient to display.

When I want to display a list of Cases, the user property should be formatted as the user's username and not their ID, but this information is not available inside the case. It is difficult to retrieve usernames inside the case repository, since it is provided by a separate user repository.

At the moment, I am doing it by creating a DisplayFor(...) template for a case's AssignedUser property and building a dictionary of users in the HttpContext at OnActionExecuting which is then referenced by the template and translated to a username. This works fairly well, but following this approach, I am populating the HttpContext at every request with a number of dictionaries (Branches, Products, etc.) that are not always necessary.

Instinctively, I should pass the user data in the view model, but that means retrieving it properly. What is the best-practice in ASP.NET-MVC without doing unnecessary queries and populating the HttpContext with useless data?


I ended up using AutoMapper to populate view models for display and map user IDs to user names, et cetera.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜