private IEnumerable<Teaser> GetEntityItems(EntityModel entity)
 {
     List<Teaser> res = new List<Teaser>();
     //1. Check if entity is a teaser, if add it
     if (entity is Teaser)
     {
         res.Add((Teaser)entity);
     }
     else
     {
         //2. Second check if entity type is (semantically) a list, and if so, get its list items
         List<Teaser> items = GetTeaserListFromSemantics(entity);
         if (items != null)
         {
             res = items;
         }
         else
         {
             //3. Last resort, try to find some suitable properties using reflection
             Teaser teaser = new Teaser();
             foreach (PropertyInfo pi in entity.GetType().GetProperties())
             {
                 switch (pi.Name)
                 {
                     case "Headline":
                     case "Name":
                         teaser.Headline = pi.GetValue(entity) as String;
                         break;
                     case "Date":
                         DateTime? date = pi.GetValue(entity) as DateTime?;
                         if (date != null)
                             teaser.Date = date;
                         break;
                     case "Description":
                         teaser.Text = pi.GetValue(entity) as String;
                         break;
                     case "Link":
                         teaser.Link = pi.GetValue(entity) as Link;
                         break;
                     case "Url":
                         string url = pi.GetValue(entity) as String;
                         if (url != null)
                             teaser.Link = new Link { Url = url };
                         break;
                 }
             }
             if (teaser.Headline != null || teaser.Text != null || teaser.Link != null)
             {
                 res.Add(teaser);
             }
         }
     }
     return res;
 }
        /// <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>
        protected bool MapRequestFormData(EntityModel model)
        {
            if (Request.HttpMethod != "POST")
            {
                return false;
            }

            // CSRF protection: If the anti CSRF cookie is present, a matching token must be in the form data too.
            const string antiCsrfToken = "__RequestVerificationToken";
            if (Request.Cookies[antiCsrfToken] != null)
            {
                AntiForgery.Validate();
            }

            Type modelType = model.GetType();
            foreach (string formField in Request.Form)
            {
                if (formField == antiCsrfToken)
                {
                    // This is not a form field, but the anti CSRF token (already validated above).
                    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];

                ValidationAttribute validationAttr = modelProperty.GetCustomAttribute<ValidationAttribute>();
                if (validationAttr != null)
                {
                    try
                    {
                        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))
                    {
                        // The @Html.CheckBoxFor method includes a hidden field with the original checkbox state, resulting in two boolean values (comma separated)
                        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;
        }
 private List<Teaser> GetTeaserListFromSemantics(EntityModel entity)
 {
     Type type = entity.GetType();
     bool isList = false;
     foreach (object attr in type.GetCustomAttributes(true))
     {
         if (attr is SemanticEntityAttribute)
         {
             SemanticEntityAttribute semantics = (SemanticEntityAttribute)attr;
             isList = semantics.Vocab == ViewModel.SchemaOrgVocabulary && semantics.EntityName == "ItemList";
             if (isList)
             {
                 break;
             }
         }
     }
     if (isList)
     {
         foreach(PropertyInfo pi in type.GetProperties())
         {
             if (pi.PropertyType == typeof(List<Teaser>))
             {
                  return pi.GetValue(entity) as List<Teaser>;
             }
         }
     }
     return null;
 }