Unobtrusive Validation Demo

 
  •  
Yes
No

click the submit button to see the validation

Unobtrusive/Index.cshtml
@using (Html.BeginForm())
{
@Html.EditorFor(o => o.Name)
@Html.EditorFor(o => o.Number)
@Html.EditorFor(o => o.MealAuto)
@Html.EditorFor(o => o.Date)
@Html.EditorFor(o => o.Categories)
@Html.EditorFor(o => o.Category2)
@Html.EditorFor(o => o.CategoryFo)
@Html.EditorFor(o => o.Meal)
@Html.EditorFor(o => o.Meals)
@Html.EditorFor(o => o.MealsOdd)
@Html.EditorFor(o => o.CategoryBgrId)
@Html.EditorFor(o => o.CategoriesBgcIds)
@Html.EditorFor(o => o.CategoriesMultiIds)
@Html.EditorFor(o => o.ColorId)
@Html.EditorFor(o => o.MealComboId)
@Html.EditorFor(o => o.Organic)
@Html.EditorFor(o => o.Organic2)
@Html.EditorFor(o => o.Organic3)
@Html.EditorFor(o => o.OrganicSim)
<div class="esubmit">
<input type="submit" value="submit" class="awe-btn mbtn" />
</div>
}
<h4>click the submit button to see the validation</h4>
ViewModels/Input/UnobtrusiveInput.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

using Omu.AwesomeMvc;

namespace AwesomeMvcDemo.ViewModels.Input
{
public class UnobtrusiveInput
{
[Required]
[AdditionalMetadata("placeholder", "write something")]
[AdditionalMetadata("maxLength", 13)]
public string Name { get; set; }

[Required]
[AdditionalMetadata("placeholder", "only numbers here")]
[AdditionalMetadata("decimals", 2)]
public float? Number { get; set; }

[Required]
[UIHint("Autocomplete")]
[AdditionalMetadata("placeholder", "try Mango")]
[AdditionalMetadata("controller", "MealAutocomplete")]
[DisplayName("Meal auto")]
public string MealAuto { get; set; }

[Required]
[AdditionalMetadata("Placeholder", "pick a date")]
public DateTime? Date { get; set; }

[Required]
[UIHint("AjaxCheckboxList")]
[AweUrl(Action = "GetCategories", Controller = "Data")]
public IEnumerable<int> Categories { get; set; }

[Required]
[UIHint("AjaxRadioList")]
[AweUrl(Action = "GetCategories", Controller = "Data")]
public int? Category2 { get; set; }

[Required]
[DisplayName("Category")]
[AweUrl(Action = "GetCategoriesFirstOption", Controller = "Data")]
[UIHint("AjaxDropdown")]
public int? CategoryFo { get; set; }

[Required]
[UIHint("Lookup")]
[AdditionalMetadata("ClearButton", true)]
public int? Meal { get; set; }

[Required]
[UIHint("MultiLookup")]
[AdditionalMetadata("ClearButton", true)]
public IEnumerable<int> Meals { get; set; }

[Required]
[UIHint("Odropdown")]
[DisplayName("Odropdown")]
[AweUrl(Action = "GetAllMeals", Controller = "Data")]
public int? MealsOdd { get; set; }

[Required]
[UIHint("ButtonGroupRadio")]
[DisplayName("ButtonGroup")]
[AweUrl(Action = "GetCategories", Controller = "Data")]
public int? CategoryBgrId { get; set; }

[Required]
[UIHint("ButtonGroupCheckbox")]
[DisplayName("ButtonGroup Multi")]
[AweUrl(Action = "GetCategories", Controller = "Data")]
public int[] CategoriesBgcIds { get; set; }

[Required]
[UIHint("Multiselect")]
[DisplayName("Categories Multi")]
[AweUrl(Action = "GetCategories", Controller = "Data")]
public int[] CategoriesMultiIds { get; set; }

[Required]
[UIHint("ColorDropdown")]
[DisplayName("Color")]
public string ColorId { get; set; }

[Required]
[UIHint("Combobox")]
[DisplayName("Meal Combo")]
[AweUrl(Action = "GetAllMeals", Controller = "Data")]
public string MealComboId { get; set; }

public bool Organic { get; set; }

[UIHint("ChkEmpVal")]
[Required(ErrorMessage = "This must be checked, in order to submit the form")]
public bool Organic2 { get; set; }

[UIHint("TogglEmpVal")]
[Required(ErrorMessage = "This must be Yes, in order to submit the form")]
public bool Organic3 { get; set; }

[UIHint("SchkEmpVal")]
[Required(ErrorMessage = "This must be checked, in order to submit the form")]
public bool OrganicSim { get; set; }
}
}



Comments