有没有办法以某种方式组合来自两个模型的数据,然后在编辑操作的上下文中将它们两个映射到同一个viewModel?
我从未在使用Entity Framework 6.1.3的ASP.NET MVC中的编辑操作中一次更新多个表。这是布局:
我有一个名为“地址”的数据库表,其中包含StreetNumber,StreetName,City,State,ZipCode的字段。它与另一个名为Bars的表具有一对一关系。同样,一个栏只能有一个地址,一个地址只能有一个栏。
因为我将这些数据存储在两个单独的表中,所以我很难尝试成功实现一个Edit动作,该动作从一个表单(BarForm)获取数据,并且应该同时更新Bar和地址数据库表。看我的代码:
BarController
public ActionResult Edit(int id)
{
var bar = _context.Bars.SingleOrDefault(m => m.Id == id);
var address = _context.Addresses.SingleOrDefault(a => a.BarId == id);
//Make sure that the id actually exists:
if (bar == null)
{
return HttpNotFound();
}
var viewModel = Mapper.Map<Bar, BarFormViewModel>(bar, new BarFormViewModel());
if (address == null)
{
address = new Address();
}
Mapper.Map<Address, BarFormViewModel>(address, viewModel);
viewModel.IsNew = false;
return View("BarForm", viewModel);
}
[ValidateAntiForgeryToken]
public ActionResult Save(BarFormViewModel bar)
{
if (!ModelState.IsValid)
{
var viewModel = Mapper.Map<BarFormViewModel, BarFormViewModel>(bar, new BarFormViewModel());
viewModel.IsNew = false;
return View("BarForm", viewModel);
}
if (bar.Id == 0)
{
var newbar = Mapper.Map<BarFormViewModel, Bar>(bar);
newbar.LastUpdated = DateTime.UtcNow;
_context.Bars.Add(newbar);
var addressToAdd = Mapper.Map<BarFormViewModel, Address>(bar);
_context.Addresses.Add(addressToAdd);
}
else
{
var barInDb = _context.Bars.Single(b => b.Id == bar.Id);
var addressInDb = _context.Addresses.Single(a => a.BarId == bar.Id);
Mapper.Map<BarFormViewModel, Bar>(bar, barInDb);
Mapper.Map<BarFormViewModel, Address>(bar, addressInDb);
}
_context.SaveChanges();
return RedirectToAction("Index", "Bar");
}
域名模型:
public class Bar
{
public int Id { get; set; }
public string Name { get; set; }
[Required]
public string GooglePlaceId { get; set; }
public string SundayDiscounts { get; set; }
public string MondayDiscounts { get; set; }
public string TuesdayDiscounts { get; set; }
public string WednesdayDiscounts { get; set; }
public string ThursdayDiscounts { get; set; }
public string FridayDiscounts { get; set; }
public string SaturdayDiscounts { get; set; }
[Display(Name = "Last Updated")]
public DateTime LastUpdated { get; set; }
}
public class Address
{
public int Id { get; set; }
public int? Number { get; set; }
public string StreetName { get; set; }
public string City { get; set; }
public string State { get; set; }
[Required]
public int ZipCode { get; set; }
public Bar Bar { get; set; }
public int BarId { get; set; }
}
查看包含地址和栏属性的模型:
{
public class BarFormViewModel
{
public int? Id { get; set; }
public string Name { get; set; }
[Required]
[Display(Name = "Google Place ID")]
public string GooglePlaceId { get; set; }
[Display(Name = "Sunday Happy Hour Info:")]
public string SundayDiscounts { get; set; }
[Display(Name = "Monday Happy Hour Info:")]
public string MondayDiscounts { get; set; }
[Display(Name = "Tuesday Happy Hour Info:")]
public string TuesdayDiscounts { get; set; }
[Display(Name = "Wednesday Happy Hour Info:")]
public string WednesdayDiscounts { get; set; }
[Display(Name = "Thursday Happy Hour Info:")]
public string ThursdayDiscounts { get; set; }
[Display(Name = "Friday Happy Hour Info:")]
public string FridayDiscounts { get; set; }
[Display(Name = "Saturday Happy Hour Info:")]
public string SaturdayDiscounts { get; set; }
[Display(Name = "Last Updated")]
public DateTime? LastUpdated { get; set; }
//Address Model Info
public Address Address { get; set; }
public int? AddressId { get; set; }
[RegularExpression("([1-9][0-9]*)", ErrorMessage = "Must be a number")]
public int? Number { get; set; }
public string StreetName { get; set; }
public string City { get; set; }
public string State { get; set; }
[Required]
public int? ZipCode { get; set; }
public bool IsNew { get; set; }
}
这里的问题是我使用此设置获取一个空的AddressId,这会在Save操作运行时导致异常。这是因为BarForm视图传递了一个已从Bar对象映射的ViewModel,而Bar域模型实际上没有Address信息,因为它不是Address model / table。
有没有办法以某种方式组合来自地址和条形模型的数据,然后将它们两个映射到同一个viewModel?
我在“保存”操作中不断为此行获取“序列包含无元素”错误:
var addressInDb = _context.Addresses.Single(a => a.Id == bar.AddressId);
我也尝试过:
var addressInDb = _context.Addresses.Single(a => a.BarId == bar.Id);
都没有工作。我理解错误说的是什么,并且还检查了我隐藏的Addressid字段的实际HTML并且它是空白的......请参阅我的BarForm视图中的代码:
@Html.HiddenFor(m => m.Id)
@Html.HiddenFor(m => m.AddressId)
@Html.AntiForgeryToken()
答案 0 :(得分:0)
删除new BarFormViewModel()
作为映射调用中的第二个参数,因为它不是必需的。
在你的post动作中,在if语句中检查ModelState是否有效以及bar.Id == 0,bar是否已经是视图模型,所以不需要映射。
当您创建AutoMapper映射时,您必须创建自定义属性映射,因为Address.Id属性不会自动映射到AddressId属性,因为名称不相同。
AutoMapper.Mapper.CreateMap<Address, BarFormViewModel>()
.ForMember(dest => dest.AddressId, o => o.MapFrom(source => source.Id));
然后对逆映射执行相同操作。