/// <summary>
 /// Constructor
 /// </summary>
 public ViewObjectDataSource(Configuration configuration)
 {
     TypeName          = GetType().ToString();
     SelectCountMethod = "SelectCount";
     SelectMethod      = "Select";
     EnablePaging      = true;
     Config            = configuration;
     SelectParameters.Add("configuration", TypeCode.Object, "");
     Selecting += OnSelecting;
 }
示例#2
0
        /// <summary>
        /// Evaluates the method parameters using model binding.
        /// </summary>
        /// <param name="dataSourceOperation">The datasource operation for which parameters are being evaluated.</param>
        /// <param name="modelDataSourceMethod">The ModelDataSourceMethod object for which the Parameter collection is being evaluated. The MethodInfo property should already be set on this object.</param>
        /// <param name="controlValues">The values from the data bound control.</param>
        /// <param name="isPageLoadComplete">This must be set to true only when this method is called in Page's LoadComplete event handler
        /// to evaluate the select method parameters that use custom value providers so that we can identify any changes
        /// to those and mark the data-bound control for data binding if necessary.</param>
        protected virtual void EvaluateMethodParameters(DataSourceOperation dataSourceOperation, ModelDataSourceMethod modelDataSourceMethod, IDictionary controlValues, bool isPageLoadComplete)
        {
            Debug.Assert(_owner.DataControl.Page != null);
            Debug.Assert(_owner.DataControl.TemplateControl != null);

            MethodInfo actionMethod = modelDataSourceMethod.MethodInfo;

            IModelBinder binder = ModelBinders.Binders.DefaultBinder;

            IValueProvider dataBoundControlValueProvider = GetValueProviderFromDictionary(controlValues);

            ModelBindingExecutionContext modelBindingExecutionContext = _owner.DataControl.Page.ModelBindingExecutionContext;

            //This is used by ControlValueProvider later.
            modelBindingExecutionContext.PublishService <Control>(_owner.DataControl);

            //This is done for the TryUpdateModel to work inside a Data Method.
            if (dataSourceOperation != DataSourceOperation.Select)
            {
                _owner.DataControl.Page.SetActiveValueProvider(dataBoundControlValueProvider);
            }

            foreach (ParameterInfo parameterInfo in actionMethod.GetParameters())
            {
                object value     = null;
                string modelName = parameterInfo.Name;

                if (parameterInfo.ParameterType == typeof(ModelMethodContext))
                {
                    //ModelMethodContext is a special parameter we pass in for enabling developer to call
                    //TryUpdateModel when Select/Update/Delete/InsertMethods are on a custom class.
                    value = new ModelMethodContext(_owner.DataControl.Page);
                }
                //Do not attempt model binding the out parameters
                else if (!parameterInfo.IsOut)
                {
                    bool           validateRequest;
                    IValueProvider customValueProvider = GetCustomValueProvider(modelBindingExecutionContext, parameterInfo, ref modelName, out validateRequest);

                    //When we are evaluating the parameter at the time of page load, we do not want to populate the actual ModelState
                    //because there will be another evaluation at data-binding causing duplicate errors if model validation fails.
                    ModelStateDictionary modelState = isPageLoadComplete ? new ModelStateDictionary() : _owner.DataControl.Page.ModelState;

                    ModelBindingContext bindingContext = new ModelBindingContext()
                    {
                        ModelBinderProviders = ModelBinderProviders.Providers,
                        ModelMetadata        = ModelMetadataProviders.Current.GetMetadataForType(null, parameterInfo.ParameterType),
                        ModelState           = modelState,
                        ModelName            = modelName,
                        ValueProvider        = customValueProvider,
                        ValidateRequest      = validateRequest
                    };

                    //Select parameters that take custom values providers are tracked by ViewState so that
                    //we can detect any changes from previous page request and mark the data bound control for data binding if necessary.
                    if (dataSourceOperation == DataSourceOperation.Select && customValueProvider != null && parameterInfo.ParameterType.IsSerializable)
                    {
                        if (!SelectParameters.ContainsKey(parameterInfo.Name))
                        {
                            SelectParameters.Add(parameterInfo.Name, new MethodParameterValue());
                        }

                        if (binder.BindModel(modelBindingExecutionContext, bindingContext))
                        {
                            value = bindingContext.Model;
                        }
                        SelectParameters[parameterInfo.Name].UpdateValue(value);
                    }
                    else
                    {
                        if (isPageLoadComplete)
                        {
                            Debug.Assert(dataSourceOperation == DataSourceOperation.Select, "Only Select Operation should have been done immediately after page load");
                            //When this method is called as part of Page's LoadComplete event handler we do not have values in defaultValueProvider
                            //(i.e., values from DataBoundControl), so we need not evaluate the parameters values.
                            continue;
                        }

                        if (customValueProvider == null)
                        {
                            bindingContext.ValueProvider = dataBoundControlValueProvider;
                        }

                        if (binder.BindModel(modelBindingExecutionContext, bindingContext))
                        {
                            value = bindingContext.Model;
                        }
                    }

                    if (!isPageLoadComplete)
                    {
                        ValidateParameterValue(parameterInfo, value, actionMethod);
                    }
                }
                modelDataSourceMethod.Parameters.Add(parameterInfo.Name, value);
            }
        }