asp.net-mvc – 将Viewmodel数据保存到ASP.NET MVC中的数据库
发布时间:2020-12-16 04:25:19 所属栏目:asp.Net 来源:网络整理
导读:我是ASP.net MVC的新手,我使用viewmodel而不是viewbags填充我的下拉菜单,因为我看到大多数人推荐他们.我有一个光滑的用户界面,可以进行级联下拉和自动填充(此处未显示),但我似乎无法将我的数据保存回数据库. 楷模: public partial class Car { public int C
我是ASP.net MVC的新手,我使用viewmodel而不是viewbags填充我的下拉菜单,因为我看到大多数人推荐他们.我有一个光滑的用户界面,可以进行级联下拉和自动填充(此处未显示),但我似乎无法将我的数据保存回数据库.
楷模: public partial class Car { public int CarID { get; set; } public string CarName { get; set; } public int ModelID { get; set; } public int ManufacturerID { get; set; } public int CarColorID { get; set; } public Nullable<decimal> Price { get; set; } public string Description { get; set; } public virtual CarColor CarColor { get; set; } public virtual Manufacturer Manufacturer { get; set; } public virtual CarModel CarModel { get; set; } } public partial class CarColor { public CarColor() { this.Cars = new HashSet<Car>(); } public int ColorID { get; set; } public string ColorName { get; set; } public virtual ICollection<Car> Cars { get; set; } } public partial class CarModel { public CarModel() { this.Cars = new HashSet<Car>(); } public int CarModelID { get; set; } public int ManufacturerID { get; set; } public string CarModelName { get; set; } public virtual ICollection<Car> Cars { get; set; } public virtual Manufacturer Manufacturer { get; set; } } public partial class Manufacturer { public Manufacturer() { this.Cars = new HashSet<Car>(); this.Manufacturer1 = new HashSet<Manufacturer>(); this.CarModels = new HashSet<CarModel>(); } public int ManufacturerID { get; set; } public string ManufacturerName { get; set; } public Nullable<int> ParentID { get; set; } public virtual ICollection<Car> Cars { get; set; } public virtual ICollection<Manufacturer> Manufacturer1 { get; set; } public virtual Manufacturer Manufacturer2 { get; set; } public virtual ICollection<CarModel> CarModels { get; set; } } 视图模型: public class AnotherTestViewModel { public Car car { get; set; } public IEnumerable<SelectListItem> CarModels { get; set; } public IEnumerable<SelectListItem> Manufacturers { get; set; } public IEnumerable<SelectListItem> CarColors { get; set; } } 控制器: public ActionResult Create() { var model = new AnotherTestViewModel(); using (new CarTestEntities()) { model.CarModels = db.CarModels.ToList().Select(x => new SelectListItem { Value = x.CarModelID.ToString(),Text = x.CarModelName }); model.Manufacturers = db.Manufacturers.ToList().Select(x => new SelectListItem { Value = x.ManufacturerID.ToString(),Text = x.ManufacturerName }); model.CarColors = db.CarColors.ToList().Select(x => new SelectListItem { Value = x.ColorID.ToString(),Text = x.ColorName }); } return View(model); } // // POST: /AnotherTest/Create [HttpPost] public ActionResult Create(AnotherTestViewModel model) { if (ModelState.IsValid) { db.Entry(model).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Details","AnotherTestViewModel",new { id = model.car.CarID }); } return View(); } 我看到了一些使用Automapper的建议,因为EntityState.Modified不起作用,但我不知道如何配置它,因为使用下面的代码不起作用. Mapper.CreateMap<AnotherTestViewModel,Car>(); Mapper.CreateMap<Car,AnotherTestViewModel>(); var newCar = Mapper.Map<AnotherTestViewModel,Car>(model); 有任何想法吗? 解决方法
您的视图模型不应与数据库交互.视图模型应仅用于表示层(用户界面) – 因此称为“视图”模型.您应该有另一个与数据库交互的模型(数据模型).然后,您应该有一些类型的服务层来处理视图模型和数据模型之间的转换(反之亦然).您的数据模型是由Entity Framework生成的模型(我假设您正在使用它).要处理数据库更新,您需要实例化数据上下文,从数据库中获取数据实体,对该实体进行更改,并在该数据上下文中调用保存更改.数据上下文将跟踪实体的所有更改,并在您调用“保存更改”时对数据库应用必要的更改.
例: public void UpdateCar(CarViewModel viewModel) { using (DataContext context = new DataContext()) { CarEntity dataModel = context.CarEntities.where(x => x.Id == viewModel.Id).First(); dataModel.Name = viewModel.Name; dataModel.Type = viewModel.Type; context.SaveChanges(); } } 在此示例中,上下文将跟踪对“dataModel”的任何更改.调用“context.SaveChanges”时,这些更改将自动应用于数据库. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 四种为HttpClient添加默认请求报头的解决方案
- asp.net-mvc – 用于MVC的程序集中的资源的Bundle Script和
- .NET Core 3.0及ASP.NET Core 3.0 前瞻
- asp.net-mvc – 维护消息完整性
- .NET Core容器化@Docker
- ASP.NET中MVC使用AJAX调用JsonResult方法并返回自定义错误信
- asp-classic – VBScript似乎是来自嵌套作用域的GC’对象,顺
- asp.net – 标签中的换行符
- asp.net – 连接池的最大和最小大小是多少ADO.Net在连接字符
- asp.net-mvc – MVC 3 – 使用List类型属性绑定到复杂类型
推荐文章
站长推荐
- 如何在ASP.NET MVC中测试自定义模型绑定器?
- asp.net – UserHostAddress提供错误的IP
- asp.net-mvc – ASP.NET核心映射到静态文件处理程
- asp.net-mvc – WS-FED身份验证(ADFS)和ASP.NET
- asp.net – 具有html5中的文本以外的输入类型的U
- asp.net – 在Web应用程序项目中使用codefile
- asp.net – 输出缓存使用BOTH varybyparam和vary
- .net 4中的ASP.NET Web服务应用程序
- 在ASP.NET MVC中模拟User.Identity
- asp.net-web-api – 设置默认的WebAPI格式化程序
热点阅读