以下是我的2个型号:
VendorMapMessage:
public int ID { get; set; }
public string Name { get; set; }
public int VendorMapID { get; set; }
public VendorMap VendorMap { get; set; }
public int DocumentTypeID { get; set; }
public DocumentType DocumentType { get; set; }
public int IncidentTypeID { get; set; }
public IncidentType IncidentType { get; set; }
public ICollection<VendorMapMessagePropertyService> VendorMapMessagePropertyServices { get; set; }
public string MessageContent { get; set; }'
VendorMapMessagePropertyService:
public int ID { get; set; }
public int VendorMapMessageID { get; set; }
public VendorMapMessage VendorMapMessage { get; set; }
public int PropertyTypeID { get; set; }
public PropertyType PropertyType { get; set; }
public int ServiceTypeID { get; set; }
public ServiceType ServiceType { get; set; }
这是我的业务逻辑:
public VendorMapMessage SaveConfirmationMessage(VendorMapMessage source)
{
VendorMapMessage result = null;
try
{
using (var db = new ClientAdminContext())
{
VendorMapMessage temp = null;
if (source.ID == default(int))
{
temp = new VendorMapMessage();
db.VendorMapMessages.Add(temp);
//techdebt remove
temp.ID = 84;
temp.IncidentTypeID = source.IncidentTypeID;
temp.DocumentTypeID = source.DocumentTypeID;
temp.MessageContent = source.MessageContent;
}
else
{
temp = db.VendorMapMessages.Single(o => o.ID == source.ID);
}
temp.IncidentTypeID = source.IncidentTypeID;
temp.DocumentTypeID = source.DocumentTypeID;
temp.MessageContent = source.MessageContent;
db.SaveChanges();
result = temp;
}
}
catch (Exception ex)
{
//log
throw;
}
return result;
以下是我的观点:
<label for="description" class="col-sm-4">Property Type</label>
<div class="col-md-7">
<select class="form-control" name="PropertyTypeID" multiple id="prop_type">
@foreach (PropertyType item in propertyTypes)
{
if (@Model.VendorMapMessagePropertyServices.Any(o => o.PropertyTypeID == item.ID))
{
<option selected="selected" value=@item.ID>@item.Name</option>
}
else
{
<option value=@item.ID>@item.Name</option>
}
}
</select>
</div>
ICollection VendorMapMessagePropertyService显示所有可用的属性类型。但我无法保存列出的属性类型。我错过了业务逻辑中的一行,它必须使用vendormapmessageID作为主键并链接到vendormapmessagepropertyservices的propertytypeID。
混淆连接vendormapmessagepropertyservice中的PropertytypeID。 (我知道模型名称是垃圾。我不认为这会变得复杂)
答案 0 :(得分:0)
您的问题不太明确,但听起来您想要保存与VendorMapMessagePropertyService
绑定的VendorMapMessage
的集合?如果是这样,你可以这样做:
temp.VendorMapMessagePropertyServices = source.VendorMapMessagePropertyServices;
保存对上下文的更改后,这将保存新的VendorMapMessagePropertyService
并将其与您的VendorMapMessage
相关联。
根据评论进行修改:您希望更新已存在的PropertyTypeID
上的VendorMapMessagePropertyService
吗?如果是这样,你需要从上下文中获取它们,然后更新它们,如下所示:
// Get existing property services in the database
IEnumerable<VendorMapMessagePropertyService> allExistingPropertyServices = db.VendorMapMessagePropertyServices;
// Loop through property services from the source...
foreach (VendorMapMessagePropertyService propertyService in source.VendorMapMessagePropertyServices) {
// Find existing property service
VendorMapMessagePropertyService existingPropertyService = allExistingPropertyServices.FirstOrDefault(x => x.ID == propertyService.ID);
// If found, update it
if (existingPropertyService != null) {
existingPropertyService.PropertyTypeID = propertyService.PropertyTypeID;
}
}
如果您还想添加源中但在数据库中不存在的属性服务,则需要修改此项。