asp.net-mvc-3 – 实体框架:ViewModel到域模型
发布时间:2020-12-16 07:32:15 所属栏目:asp.Net 来源:网络整理
导读:我正在建立一个MVC 3网站.我有一个看起来像这样的模型: public class Survey{ [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; } public string Name { get; set; } public string Description { get; set; } pub
我正在建立一个MVC 3网站.我有一个看起来像这样的模型:
public class Survey { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; } public string Name { get; set; } public string Description { get; set; } public DateTime DateStart { get; set; } public DateTime DateEnd { get; set; } // Not in view public DateTime DateCreated { get; set; } // Not in view public DateTime DateModified { get; set; } } 基于此,我还有一个View Model来编辑调查信息: public class SurveyEditViewModel { public Guid Id { get; set; } public string Name { get; set; } public string Description { get; set; } public DateTime DateStart { get; set; } public DateTime DateEnd { get; set; } } 当用户完成编辑时,我希望保留更改.这是我的控制器发布动作: [HttpPost] public ActionResult Edit(SurveyEditViewModel model) { // Map the view model to a domain model using AutoMapper Survey survey = Mapper.Map<SurveyEditViewModel,Survey>(model); // Update the changes _repository.Update(survey); // Return to the overview page return RedirectToAction("Index"); } 在我的存储库(它现在是通用的)我有以下代码: public void Update(E entity) { using (ABCDataContext context = new ABCDataContext()) { context.Entry(entity).State = System.Data.EntityState.Modified; context.SaveChanges(); } } 执行此操作时,我收到以下错误:“存储更新,插入或删除语句影响了意外的行数(0).实体可能已被修改或删除,因为实体已加载.刷新ObjectStateManager条目.” 我想这是可以预料的.从视图模型到模型的映射不会给我一个完整的Survey对象. 我可以修改我的控制器看起来像这样.然后它的工作原理: [HttpPost] public ActionResult Edit(SurveyEditViewModel model) { // Map the model to a real survey survey = _repository.Find(model.Id); survey.Name = model.Name; survey.Description = model.Description; survey.DateStart = model.DateStart; survey.DateEnd = model.DateEnd; // Update the changes _repository.Update(survey); // Return to the overview page return RedirectToAction("Index"); } 但我想知道是否有更好的方法可用? 解决方法[HttpPost] public ActionResult Edit(SurveyEditViewModel model) { // Fetch the domain model to update var survey = _repository.Find(model.Id); // Map only the properties that are present in the view model // and keep the other domain properties intact Mapper.Map<SurveyEditViewModel,Survey>(model,survey); // Update the changes _repository.Update(survey); // Return to the overview page return RedirectToAction("Index"); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 为什么调试器不会在我的ASP.NET应用程序的断点处停止?
- asp.net – 将RSS pubDate格式化为.NET DateTime
- asp.net – 为什么有#!在我的角度应用程序的网址中
- asp.net-mvc – 客户端存储的身份验证令牌在哪里?
- asp.net-mvc – LINQ到EF有什么问题?
- asp.net-mvc – Viewmodel和动态菜单最佳实践 – ASP.NET M
- asp.net-mvc – ASP.NET MVC中的Crystal Reports
- ASP.NET Core中的OWASP Top 10 十大风险-SQL注入
- asp.net-mvc-3 – OutputCache和自定义gzip压缩过滤器
- asp.net – 如何在警告框中显示验证控件的错误消息?
推荐文章
站长推荐
- asp.net-mvc – .net MVC控制器动作方法的属性
- asp.net – 在Visual Studio中查找CSS类引用的快
- asp.net-mvc-3 – 与SQL Server建立连接时发生与
- 如何在aspx页面中隐藏ASP.NET自定义控件的属性?
- 使用ASP.NET Identity实现基于声明的授权,高级篇
- asp.net – 动态CheckBoxFor有一些禁用
- asp.net – 依赖关系未安装在Visual Studio中
- asp.net – 哪种模式最匹配场景详细,是不是很好的
- iis – 如何调试w3wp.exe随机崩溃的原因?
- asp.net – Silverlight中的多任务处理和多线程处
热点阅读