/// <summary> /// Saves an attribute value. /// </summary> /// <param name="model">The model.</param> /// <param name="attribute">The attribute.</param> /// <param name="newValues">The new values.</param> /// <param name="personId">The person id.</param> public static void SaveAttributeValues(IHasAttributes model, Rock.Web.Cache.AttributeCache attribute, List <Rock.Model.AttributeValueDto> newValues, int?personId) { Model.AttributeValueService attributeValueService = new Model.AttributeValueService(); var attributeValues = attributeValueService.GetByAttributeIdAndEntityId(attribute.Id, model.Id).ToList(); int i = 0; while (i < attributeValues.Count || i < newValues.Count) { Rock.Model.AttributeValue attributeValue; if (i < attributeValues.Count) { attributeValue = attributeValues[i]; } else { attributeValue = new Rock.Model.AttributeValue(); attributeValue.AttributeId = attribute.Id; attributeValue.EntityId = model.Id; attributeValue.Order = i; attributeValueService.Add(attributeValue, personId); } if (i >= newValues.Count) { attributeValueService.Delete(attributeValue, personId); } else { if (attributeValue.Value != newValues[i].Value) { attributeValue.Value = newValues[i].Value; } newValues[i] = new Rock.Model.AttributeValueDto(attributeValue); } attributeValueService.Save(attributeValue, personId); i++; } model.AttributeValues[attribute.Key] = newValues; }
/// <summary> /// Saves an attribute value. /// </summary> /// <param name="model">The model.</param> /// <param name="attribute">The attribute.</param> /// <param name="newValue">The new value.</param> /// <param name="personId">The person id.</param> public static void SaveAttributeValue(IHasAttributes model, Rock.Web.Cache.AttributeCache attribute, string newValue, int?personId) { Model.AttributeValueService attributeValueService = new Model.AttributeValueService(); var attributeValue = attributeValueService.GetByAttributeIdAndEntityId(attribute.Id, model.Id).FirstOrDefault(); if (attributeValue == null) { attributeValue = new Rock.Model.AttributeValue(); attributeValue.AttributeId = attribute.Id; attributeValue.EntityId = model.Id; attributeValue.Order = 0; attributeValueService.Add(attributeValue, personId); } attributeValue.Value = newValue; attributeValueService.Save(attributeValue, personId); model.AttributeValues[attribute.Key] = new List <Rock.Model.AttributeValueDto>() { new Rock.Model.AttributeValueDto(attributeValue) }; }