In my ASP.NET MVC app how can I determine if any property in my domain Model has changed?

吃可爱长大的小学妹 提交于 2019-12-12 06:33:36

问题


I am working on the MVC prototype of a very large and complex ASP.NET 2.0 Webforms based business product suite for an organization. One of the capabilities inbuilt into the existing app is that it only hits the database to store a result set if the user made any changes at all on the Webforms UI. The web app has a complete layer built in to track this "if changed by user" thing.

I was wondering if within ASP.NET MVC there is a provision/functionality to determine what property was changed by the user on the view? The idea is simple, user sees a view, probably makes some changes and hits "Save". The controller that accepts the post needs to figure out if the state of the model was changed by the user or not and save it only if the state was changed.

Any thoughts?

PS: I am tagging this as mvc-2 and mvc-3 as it is a prototype. I am currently learning/developing this in MVC 2 but will quickly move to MVC 3 if that suits our needs better!


回答1:


I was wondering if within ASP.NET MVC there is a provision/functionality to determine what property was changed by the user on the view?

No, there is nothing to do this in MVC. WebForms does this by including the original values of fields in the view state, but in embracing the statelessness of HTTP at its core MVC doesn't do this.

If your (view) model types are regular enough you might be able to construct scaffolding to do this (and the more types you have the more benefit to do this). I've prototyped something on these lines where each view model type determines if there have been changes when updating the model type before any update to the database (and thus bypasses the update).



来源:https://stackoverflow.com/questions/5934612/in-my-asp-net-mvc-app-how-can-i-determine-if-any-property-in-my-domain-model-has

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