My Models:
public class Profile { public Profile() { FieldStatuss = new List<FieldStatus>(); } public int ProfileId { get; set; } public string OwnerID { get; set; } public string Name { get; set; } public string LastName { get; set; } public string Address { get; set; } public string City { get; set; } public string Email { get; set; } public string Facebook { get; set; } public string Twitter { get; set; } public string Department { get; set; } public string Office { get; set; } public virtual ICollection< FieldStatus> FieldStatuss { get; set; } } public class FieldStatus { public FieldStatus(){ Profile = new Profile(); } [Key] public int FieldStatusID { get; set; } public string ColumnName { get; set; }// for example :City public string EnumFieldStatusV { get; set; } //For example: EnumFieldStatus.OnlyMe public int ProfileId { get; set; } [ForeignKey("ProfileId")] public virtual Profile Profile { get; set; } } // EnumFieldStatus as a class if you need public enum EnumFieldStatus { [Display(Name = "Only Me.")] OnlyMe, [Display(Name = "Visible To Everyone.")] VisibleToEveryone, [Display(Name = "Visible To Office.")] VisibleToOffice, [Display(Name = "Visible To Department.")] VisibleToDepartment, [Display(Name = "Not Visible.")] NotVisible }
Controller
Profile profile = new Profile(); var props = profile.GetType().GetProperties(); foreach (var item in profileFieldStatus) { var statusField = item.ColumnName; EnumFieldStatus statusName = item.EnumFieldStatusV; if (statusName==EnumFieldStatus.NotVisible ) { props.Where(a => a.Name == "statusField").FirstOrDefault().SetValue(profile, null); //foreach (var propinfo in props) //{ // if (statusField == propinfo.Name) // { // propinfo.SetValue(profile, null, null); // } //} }
Fo the Views cannot figure it out how to get them in edit view with selected values foreach textbox and save them with post. For example when user creates a profile for the first time he selects facebook, twitter - VisibleToAll and email - VisibleToOffice. Later if he decides to change only his facebook to NotVisible, other dropdowns for the textboxes will have their selected values from db in the view (so dropdowns for twitter and email will keep VisibleToAll and VisibleToOffice values) and will update only facebook field's visibility , or easier to delete all the values and resave them again as they will have selected values from the db even only one dropdown changed.