我试图寻找通过EF对象的属性的方式来循环和更新这些属性的值。更具体地讲我有填充了50 dropdownlists 50场。所有50可以或可以不需要填充。
I am trying to find a way to loop through the properties of an EF object and update the values of these properties. More specifically I have 50 fields that are populated by up to 50 dropdownlists. All 50 may or may not need to be populated.
要解决这个问题我有一个中继器,将创建多达50个DDL。用户将选择每一个,然后preSS的更新按钮的值。我通过项目循环的中继器,并能保持我就在中继器迭代的次数。我也想用这个数就知道我需要更新哪些字段。例如DDL1 = FIELD1,DDL2 = FIELD2,...)。
To solve this I have a repeater that will create up to 50 DDL. The user will select the values for each one and then press an update button. I am looping through the items in the repeater and can keep count of which iteration I am on in the repeater. I want to also use this count to know what field I need to update. For instance DDL1 = FIELD1, DDL2 = FIELD2, ....).
下面是什么,我试图做一个例子:
Here is an example of what I am 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 any way to dynamically create the property name I need to update?
有一些属性集合,我可以通过索引来访问?
Is there some property collection I can access by index?
这甚至远程接近我应该如何去这件事?
Is this even remotely close to how I should be going about this?
任何及所有的帮助将非常AP preciated。谢谢你。
Any and all help will be much appreciated. Thank you.
推荐答案
您可以做这样的事情。
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
是字符串。所以,你需要的,如果需要采取类型转换的照顾。
SelectedValue
is string. So you need to take care of type conversion if needed.
相关推荐
最新文章