MVC 视图和控制器:提交表单时重新实例化实体
我们正在使用 MVC 控制器中的实体,这些实体被传递到强类型视图。
当表单发布到视图中时,我们如何使用更新的数据在控制器中重新实例化这些实体? 该表单不包含实体的所有字段,因此需要的所有数据 重新实例化实体将不会出现在模型绑定中。仅提交部分数据集。
我不会在使用发布的值更新实体之前根据键值重新加载实体,因为这是 1)效率低下 2)如果我不保留原始 RowVersion 并发字段的值,则会阻止实体框架中 POCO 对象的乐观并发
但我的问题是这是推荐的方法吗?
此问题可能适用于包含未在视图中重新提交的数据并且您不想重新加载实体的任何实体。
谢谢
We are working with entities in our MVC controllers which are passed to strongly typed views.
How do we re-instantiate these entities in the controller with updated data when the form is posted in the view?
The form does not contain all the fields of the entity so all of the data needed to
re-instantiate the entities won't be there in the model binding. Only a partial set of data will be submitted.
I dont wont to reload the entities based on key values prior to updating them with posted values as this is
1) Inefficient 2) Prevent optimistic concurrency with my POCO objects in the Entity Framework if I dont keep the value original RowVersion concurrency field
But my question is is this the recommended way?
This question could apply to any entities that contain data that isnt re-submitted in the view and you dont want to reload the entities.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只是向数据库发出请求以再次获取实体,如果您担心并发性,请检查版本号或其他内容。如果您主要关心的是效率低下,那么现在只需重新查询数据库,如果它确实最终效率低下,那么您可以使用某种类型的缓存......还有什么其他选择?
I would just make the request to the database to get the entity again, and if you're worried about concurrency, then check a version number or something. If your main concern is inefficiency, then just re-query the DB for now, and if it truly ends up being inefficient, then you can use some type of cache... what other options are there?