Sdl.Web.Mvc/Controllers/EntityController.cs
/// <summary>
/// Maps Form data (for an HTTP POST request) to properies of a given Entity Model and performs basic validation.
/// </summary>
/// <param name="model">The Entity Model to map the form data to.</param>
/// <returns><c>true</c> if there is any form data to be mapped.</returns>
Maps Form data (for an HTTP POST request) to properies of a given Entity Model and performs basic validation.
Maps Form data ( for an HTTP POST request ) to properies of a given Entity Model and performs basic validation .
protected bool MapRequestFormData(EntityModel model)
        {
            if (Request.HttpMethod != "POST")
            {
                return false;
            }
            const string antiCsrfToken = "__RequestVerificationToken";
            if (Request.Cookies[antiCsrfToken] != null)
            {
                AntiForgery.Validate();
            }
            Type modelType = model.GetType();
            foreach (string formField in Request.Form)
            {
                if (formField == antiCsrfToken)
                {
                    continue;
                }
                PropertyInfo modelProperty = modelType.GetProperty(formField);
                if (modelProperty == null)
                {
                    Log.Debug("Model [{0}] has no property for form field '{1}'", model, formField);
                    continue;
                }
                string formFieldValue = Request.Form[formField];
                var validationAttributes = modelProperty.GetCustomAttributes<ValidationAttribute>();
                try
                {
                    foreach (var validationAttr in validationAttributes)
                    {
                        validationAttr.Validate(formFieldValue, formField);
                    }
                }
                catch (ValidationException ex)
                {
                    string validationMessage = ResolveValidationMessage(ex.Message, model);
                    Log.Debug("Validation of property '{0}' failed: {1}", formField, validationMessage);
                    ModelState.AddModelError(formField, validationMessage);
                    continue;
                }
                try
                {
                    if (modelProperty.PropertyType == typeof (bool))
                    {
                        formFieldValue = formFieldValue.Split(',')[0];
                    }
                    modelProperty.SetValue(model, Convert.ChangeType(formFieldValue, modelProperty.PropertyType));
                }
                catch (Exception ex)
                {
                    Log.Debug("Failed to set Model [{0}] property '{1}' to value obtained from form data: '{2}'. {3}", model, formField, formFieldValue, ex.Message);
                    ModelState.AddModelError(formField, ex.Message);
                }
            }
            return true;
        }
protected bool MapRequestFormData ( EntityModel model ) { if ( Request . HttpMethod != " " ) { return false ; } const string antiCsrfToken = " " ; if ( Request . Cookies [ antiCsrfToken ] != null ) { AntiForgery . Validate ( ) ; } Type modelType = model . GetType ( ) ; foreach ( string formField in Request . Form ) { if ( formField == antiCsrfToken ) { continue ; } PropertyInfo modelProperty = modelType . GetProperty ( formField ) ; if ( modelProperty == null ) { Log . Debug ( " " , model , formField ) ; continue ; } string formFieldValue = Request . Form [ formField ] ; var validationAttributes = modelProperty . GetCustomAttributes < ValidationAttribute > ( ) ; try { foreach ( var validationAttr in validationAttributes ) { validationAttr . Validate ( formFieldValue , formField ) ; } } catch ( ValidationException ex ) { string validationMessage = ResolveValidationMessage ( ex . Message , model ) ; Log . Debug ( " " , formField , validationMessage ) ; ModelState . AddModelError ( formField , validationMessage ) ; continue ; } try { if ( modelProperty . PropertyType == typeof ( bool ) ) { formFieldValue = formFieldValue . Split ( ' ' ) [ 0 ] ; } modelProperty . SetValue ( model , Convert . ChangeType ( formFieldValue , modelProperty . PropertyType ) ) ; } catch ( Exception ex ) { Log . Debug ( " " , model , formField , formFieldValue , ex . Message ) ; ModelState . AddModelError ( formField , ex . Message ) ; } } return true ; }
Maps Form data (for an HTTP POST request) to properies of a given Entity Model and performs basic validation.
Maps Form data ( for an HTTP POST request ) to properies of a given Entity Model and performs basic validation .
// CSRF protection: If the anti CSRF cookie is present, a matching token must be in the form data too. // This is not a form field, but the anti CSRF token (already validated above). // The @Html.CheckBoxFor method includes a hidden field with the original checkbox state, resulting in two boolean values (comma separated)
None
model
EntityModel
The Entity Model to map the form data to.
The Entity Model to map the form data to .