Entity Framework - combining properties for updating

I am trying to find a way to iterate over the properties of an EF object and update the values ​​of these properties. More specifically, I have 50 fields that are filled up to 50 dropdownlists. All 50 may or may not need to be filled.

To solve this problem, I have a repeater that will generate up to 50 DDL. The user will select values ​​for each of them, and then click the update button. I iterate over the elements in the repeater and can count on what iteration I will be in the repeater. I also want to use this account to find out which field I need to update. For example, DDL1 = FIELD1, DDL2 = FIELD2, ....).

Here is an example of what I'm trying to do:

using (Data.Entities dataContext = new Data.Entities)
{
    var efObject = dataContext.EFData.Where(c => c.ID = [SOMEID]).First();

    int posCount = 0;
    foreach (RepeaterItem rep1 in repeaterControl.Items)
    {
        DropDownList ddlControl= (DropDownList)rep1.FindControl("ddlControl");

        //Here is where I need to update a field
        //Something like:  efObject.Field# = ddlControl.SelectedValue;
    }
}

Is there a way to dynamically create the name of the property I need to update?

- , ?

, ?

. .

+3
1

- .

var properties = typeof(EFType).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var property in properties)
{
    var control = (DropDownList)rep1.FindControl("ddlControl" + property.Name);
    property.SetValue(efObject, control.SelectedValue, null);
}

SelectedValue - . , .

+10

All Articles