ASP.NET MVC - Linq to Entities model as the ViewModel - is this good practice? [closed]

杀马特。学长 韩版系。学妹 提交于 2019-12-01 03:44:58

I would suggest reviewing the rendering code in your views and the posting code in your controllers. Are they made overly complex by the approach you are taking? If not you're probably ok to keep things as they are. If the view and controller code would be greatly simplified by introducing a custom view model then consider creating one. Custom view models essentially abstract some of that complexity that is probably getting handled somewhere else at the moment.

queen3

If your views start doing things like

<% foreach (var order in Model.Orders.Any(x => x.Products.Any(p => p.Category == "xx")) %>

then you definitely need ViewModel. You can go with

ViewData["flattened_orders"]

if you prefer magic strings, but I doubt so.

Then, there's a question of presentation attributes needed on your entities, then you need to expose all properties on them so that model binder can work... then you need additional presentation-only information like list of countries...

So, for simple applications, you may skip ViewModel. But for simple applications you can do Response.Write and manual SQL, anyway ;-)

I actually like this post about similar issue. The approach represented there might seem too "academic" at first, but it is from real projects, and the more I do ASP.NET MVC, the more I like it and get closer to it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!