public ActionResult Index(FormsAndValidationViewModel model) { if (ModelState.IsValid) { Feedback.AddMessage(FeedbackMessageType.Success, "All fields validated correctly.", "Contratulations!"); } else { Feedback.AddMessage(FeedbackMessageType.Error, "Some fields are not valid.", "Oops!"); } model = BuildFormsAndValidationViewModel(model); return(View(model)); }
private FormsAndValidationViewModel BuildFormsAndValidationViewModel(FormsAndValidationViewModel postedModel = null) { var model = new FormsAndValidationViewModel(); if (postedModel != null) { // Use a mapper to associate the previously posted information to the new model. model.InjectFrom(postedModel); } var countries = new List <object>() { new { Id = 1, Name = "United States" } }; model.CountrySelectList = new SelectList( countries, dataValueField: "Id", dataTextField: "Name", selectedValue: model.CountryId ); var states = new List <object>() { new { Id = 1, Name = "Michigan" }, new { Id = 2, Name = "New York" }, new { Id = 3, Name = "Washington" } }; model.StateSelectList = new SelectList( states, dataValueField: "Id", dataTextField: "Name", selectedValue: model.StateId ); // Build a named tuple list from payment enum model.PaymentOptions = Enum.GetValues(typeof(SamplePaymentTypesEnum)).Cast <SamplePaymentTypesEnum>().Select(r => ( Id: r, Description: r.GetDisplayName() ) ).ToList(); return(model); }
public ActionResult RedeemCode([CustomizeValidator(Properties = "PromoCode")] FormsAndValidationViewModel model) { // This shows how to test specific fields in isolation. if (ModelState.IsValid) { var code = model.PromoCode ?? string.Empty; var isValidPromoCode = "test".Like(code); var result = new PromoCodeInfoViewModel() { IsValid = isValidPromoCode, CurrentAmount = 25, DiscountValue = isValidPromoCode ? 5 : decimal.Zero, PromoCode = code.ToUpper() }; return(Json(result)); } return(JsonError(ModelState, HttpStatusCode.BadRequest)); }
private FormsAndValidationViewModel BuildFormsAndValidationViewModel(FormsAndValidationViewModel postedModel = null) { var model = new FormsAndValidationViewModel(); if (postedModel != null) { // Use a mapper to associate the previously posted information to the new model. model.InjectFrom(postedModel); } // Combo boxes expect a collection of objects to build the options list. // This could be loaded from a database or any other source. For demonstration purposes we will use the sample enum. var sampleValues = Enum.GetValues(typeof(SamplesEnum)).Cast <SamplesEnum>().Select(r => new { id = r.ToString(), value = r.ToString() }).OrderBy(e => e.id).ToList(); // This is a list that should be always available when presenting the single select options. model.SampleOptionsSingle = new SelectList( sampleValues, dataValueField: "id", // Specify the fields to be used. Here we will use the ones we created above from the enum. dataTextField: "value", selectedValue: model.SelectedSingle ); // This is a list that should be always available when presenting the multi select options. model.SampleOptionsMulti = new MultiSelectList( sampleValues, dataValueField: "id", // Specify the fields to be used. Here we will use the ones we created above from the enum. dataTextField: "value", selectedValues: model.SelectedMulti ); return(model); }