/// <summary> /// Validates the user profile. /// </summary> /// <param name="collection">The collection.</param> /// <param name="modelState">State of the model.</param> /// <param name="user">The user.</param> /// <param name="userProfile">The user profile.</param> public static void Validate(FormCollection collection, ModelStateDictionary modelState, UserProfile userProfile) { if (userProfile == null) { return; } foreach (var item in userProfile.ProfileType.ProfileHeaders) { foreach (var element in item.ProfileElements) { var elementName = String.Format("{0}_{1}", (ElementType)element.Type, element.Id); var value = collection[elementName]; if (value == null) { continue; } // check if the item is required if (element.IsRequired && String.IsNullOrEmpty(value)) { modelState.AddModelError(elementName, String.Format("The {0} field is required.", element.Title)); } else { ElementTypeUtility.ValidateElement((ElementType)element.Type, elementName, value, modelState); } } } }
public static MvcHtmlString ProfileElementRenderer(this HtmlHelper html, ProfileElement element, FormCollection collection) { var builder = new StringBuilder(); var elementName = String.Format(ElementNameFormat, (ElementType)element.Type, element.Id); var elementValue = String.Empty; if (collection != null && collection[elementName] != null) { elementValue = collection[elementName]; } builder.Append(html.Label(elementName, element.Title)); builder.Append("<br/>"); builder.Append(ElementTypeUtility.RenderElementType(html, (ElementType)element.Type, elementName, elementValue, element.ElementValues)); builder.Append(html.ValidationMessage(elementName)); return(MvcHtmlString.Create(builder.ToString())); }