Manos.Mvc.ModelBinder.TryUpdateModel C# (CSharp) Method

TryUpdateModel() public method

public TryUpdateModel ( IModelValueProvider provider, object model ) : bool
provider IModelValueProvider
model object
return bool
        public bool TryUpdateModel(IModelValueProvider provider, object model)
        {
            // Build the list of fields to be updated
            string[] field_list = BuildFieldList(provider, model);
            if (field_list == null)
            {
                throw new InvalidOperationException("Can't bind model as value provider can't provide a valid list of fields and neither an include nor exclude list was provided");
            }

            // Update all property values
            foreach (var f in field_list)
            {
                // Find the target property
                object target;
                PropertyInfo pi;
                if (!ResolveField(model, f, out target, out pi))
                {
                    throw new InvalidOperationException(string.Format("Can't bind field `{0}` to model of type `{1}`", f, model.GetType().FullName));
                }

                // Apply it's value
                BindField(f, provider, target, pi);
            }

            return ModelState.IsValid;
        }

Usage Example

Exemplo n.º 1
0
 public bool TryUpdateModel(object model, string[] include_fields=null, string[] exclude_fields=null)
 {
     ModelBinder b = new ModelBinder(ModelState);
     b.IncludeFields = include_fields;
     b.ExcludeFields = exclude_fields;
     return b.TryUpdateModel(new HttpModelValueProvider(Context), model);
 }