Example #1
0
        public static void GetModel(object viewObj, Control parentControl = null)
        {
            if (parentControl == null)
            {
                parentControl = Current;
            }
            var list = GetPostPair(viewObj, parentControl);

            if (list.Count == 0)
            {
                throw new InvalidOperationException("PostPair's empty!");
            }

            Page page = parentControl.Page;

            foreach (PostPair pair in list)
            {
                Control control = page.FindControl(pair.UniqueID);
                if (control == null)
                {
                    throw new InvalidOperationException(string.Format("GetPost:'{0}' isn't exists. Remark:如果此ID是OutputCache用户控件,那么在回发的时候可能是Null。", pair.UniqueID));
                }

                WebControl webControl = control as WebControl;
                if (webControl != null && !webControl.Enabled)
                {
                    continue;
                }
                PropertyAccess property = pair.Property;
                Type           propType = property.EntityProperty.PropertyType;
                if (!TypeHelper.IsStringOrValueType(propType))
                {
                    continue;
                }

                object value = null;
                Type   underlyingType;
                if (TypeHelper.IsNullableType(propType, out underlyingType))
                {
                    propType = underlyingType;
                }
                CheckBoxList listControl;
                if (propType.IsEnum &&
                    (listControl = control as CheckBoxList) != null && Attribute.IsDefined(propType, typeof(FlagsAttribute)))
                {
                    int flag = 0;
                    foreach (ListItem item in listControl.Items)
                    {
                        if (item.Selected)
                        {
                            int itemValue = int.Parse(item.Value);
                            flag |= itemValue;
                        }
                    }
                    value = flag;
                }
                else
                {
                    ITextControl textControl = control as ITextControl;
                    if (textControl != null)
                    {
                        value = textControl.Text;
                    }
                    else
                    {
                        ICheckBoxControl checkBoxControl = control as ICheckBoxControl;
                        if (checkBoxControl != null)
                        {
                            value = checkBoxControl.Checked;
                        }
                        else
                        {
                            HiddenField hiddenField = control as HiddenField;
                            if (hiddenField != null)
                            {
                                value = hiddenField.Value;
                            }
                        }
                    }
                }

                if (object.Equals(value, string.Empty))
                {
                    if (!property.IsNullable)
                    {
                        throw new InvalidCastException(property.MappedName);
                    }
                    property.SetValue(viewObj, null);
                }
                else
                {
                    if (property.EntityProperty.PropertyType == typeof(Guid))
                    {
                        property.SetValue(viewObj, new Guid(value.ToString()));
                    }
                    else
                    {
                        // property.ChangeType 处理了类型为Enum的情况
                        property.SetValue(viewObj, property.ChangeType(value));
                    }
                }
            }
        }