我正在制作一项功能,让管理员可以为用户配置邮件设置。有多个实体:
建筑物
服务
用户
在配置表单中,admin可以选择多个建筑物,多个服务和多个用户。因此,所有选定的用户都将收到所有选定建筑物和服务的邮件。在视图中,我有3个建筑物,服务和用户的多选列表。
也可以有多种设置。
现在我有以下实体:
public class MailingConfigSetting
{
public int Id { get; set; }
public int SettingId { get; set; }
public string SettingName { get; set; }
public virtual ICollection<MailingConfigUser> MailingConfigUsers { get; set; }
public virtual ICollection<MailingConfigBuilding> MailingConfigBuildings { get; set; }
public virtual ICollection<MailingConfigService> MailingConfigServices { get; set; }
}
public class MailingConfigUser
{
public int Id { get; set; }
public int UserId { get; set; }
public string UserName { get; set; }
}
public class MailingConfigBuilding
{
public int Id { get; set; }
public int BuildingId { get; set; }
public string BuildingName { get; set; }
}
public class MailingConfigService
{
public int Id { get; set; }
public int ServiceId { get; set; }
public string ServiceName { get; set; }
}
控制器:
[HttpPost]
public ActionResult New(MailingConfigVM mailingConfigVM)
{
if (!SessionControlService.CheckIsLoginStillTrue(_loginsService, HttpContext))
return RedirectToAction("Login", "Account");
var curUser = _userService.GetUserByEmail(User.Identity.Name);
if (!User.IsInAnyRoles("Admin"))
return RedirectToAction("Index");
if (ModelState.IsValid)
{
using (var domain = new Facility.Data.FacilityEntities())
{
var newSetting = new Facility.Model.MailingConfigSetting
{
SettingName = "testSetting"
};
domain.MailingConfigSettings.Add(newSetting);
foreach (int item in mailingConfigVM.SelectedBuildings)
{
var building = _buildingService.GetBuilding((int)item);
var newSettingBuilding = new Facility.Model.MailingConfigBuilding
{
BuildingId = building.Id,
BuildingName = building.Name,
};
domain.MailingConfigBuildings.Add(newSettingBuilding);
}
foreach (int item in mailingConfigVM.SelectedServices)
{
var service = _serviceService.GetService(item);
var newSettingService = new Facility.Model.MailingConfigService
{
ServiceId = service.Id,
ServiceName = service.Name
};
domain.MailingConfigServices.Add(newSettingService);
}
foreach (int item in mailingConfigVM.SelectedUsers)
{
var user = _userService.GetUser(item);
var newSettingUser = new Facility.Model.MailingConfigUser
{
UserId = user.Id,
UserName = user.UserName
};
domain.MailingConfigUsers.Add(newSettingUser);
}
//domain.MailingConfigBuildings.Add(newSettingBuilding);
domain.SaveChanges();
}
}
return RedirectToAction("Create", "MailingConfig");
}
视图模型:
public class MailingConfigVM
{
[Display(Name = "Buildings")]
//public IEnumerable<Building> allBuildings { get; set; }
public IEnumerable<SelectListItem> allBuildings { get; set; }
[Display(Name = "Services")]
//public IEnumerable<Facility.Model.Service> allServices { get; set; }
public IEnumerable<SelectListItem> allServices { get; set; }
[Display(Name = "Users")]
//public IEnumerable<User> allUsers { get; set; }
public IEnumerable<SelectListItem> allUsers { get; set; }
public IEnumerable<int> SelectedBuildings { get; set; }
public IEnumerable<int> SelectedServices { get; set; }
public IEnumerable<int> SelectedUsers { get; set; }
}
}
我有4张桌子:
1)建筑物
2)服务
3)用户
4)设置
建筑物,服务和用户的表格应包含设置表中的密钥。
在我保存到db的控制器中,我可以看到结果正在插入到所有表中,但它们缺少来自设置表的外键。
如何将密钥从设置表保存到这些表?