This article will demonstrate, how to make and validate cascading dropdown list with in Asp.Net MVC4 Razor application using custom server-side and client-side validation. In this article, you will learn how set selected value with in Dropdown list in MVC Razor.
How to do it..
Follow the following steps for making and validating cascading dropdown list in mvc razor.
Step 1 : Design Model
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.ComponentModel.DataAnnotations;
- using System.Web.Mvc;
- using System.Text.RegularExpressions;
- namespace Mvc4_Validate_CascadingDropdown.Models
- {
- public class RegistrationModel
- {
- [Required(ErrorMessage = "Please Enter Email Address")]
- [Display(Name = "UserName (Email Address)")]
- [RegularExpression(".+@.+\\..+", ErrorMessage = "Please Enter Correct Email Address")]
- public string UserName { get; set; }
- [Display(Name = "Country")]
- public Country Country { get; set; }
- [Display(Name = "City")]
- public City City { get; set; }
- [Required(ErrorMessage = "Please Enter Address")]
- [Display(Name = "Address")]
- [StringLength(200)]
- public string Address { get; set; }
- }
- // IClientValidatable for client side Validation
- public class MustBeSelectedAttribute : ValidationAttribute, IClientValidatable
- {
- public override bool IsValid(object value) {
- if (value == null || (int)value == 0)
- return false;
- else return true;
- }
- // Implement IClientValidatable for client side Validation
- public IEnumerable
GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { return new ModelClientValidationRule[] { new ModelClientValidationRule { ValidationType = "dropdown", ErrorMessage = this.ErrorMessage } }; } } public class Country { [MustBeSelectedAttribute(ErrorMessage = "Please Select Country")] public int? ID { get; set; } public string Name { get; set; } } public class City { [MustBeSelectedAttribute(ErrorMessage = "Please Select City")] public int? ID { get; set; } public string Name { get; set; } public int? Country { get; set; } } }
Step 2: Design View based on Model
- @model Mvc4_Validate_CascadingDropdown.Models.RegistrationModel
- @{ ViewBag.Title = "Validating Cascading Dropdownlist";
- }
Custom Validation for Cascading Dropdown List
Design Controller Based on Model & View
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using Mvc4_Validate_CascadingDropdown.Models;
- using System.Text.RegularExpressions;
- namespace Mvc4_Validate_CascadingDropdown.Controllers {
- public class HomeController : Controller {
- #region Private Methods
- void BindCountry() {
- List
lstCountry = new List { new Country { ID = null, Name = "Select" }, new Country { ID = 1, Name = "India" }, new Country { ID = 2, Name = "USA" } }; ViewBag.Country = lstCountry; } //for server side void BindCity(int? mCountry) { try { if (mCountry != 0) { //below code is only for demo, you can pick city from database int index = 1; List lstCity = new List { new City { Country = 0, ID=null, Name = "Select" }, new City { Country = 1, ID=index++, Name = "Delhi" }, new City { Country = 1, ID=index++, Name = "Lucknow" }, new City { Country = 1, ID=index++, Name = "Noida" }, new City { Country = 1, ID=index++, Name = "Guragon" }, new City { Country = 1, ID=index++, Name = "Pune" }, new City { Country = 2, ID=index++, Name = "New-York" }, new City { Country = 2, ID=index++, Name = "California" }, new City { Country = 2, ID=index++, Name = "Washington" }, new City { Country = 2, ID=index++, Name = "Vermont" }, }; var city = from c in lstCity where c.Country == mCountry || c.Country == 0 select c; ViewBag.City = city; } else { List City = new List { new City { ID = null, Name = "Select" } }; ViewBag.City = City; } } catch (Exception ex) { } } #endregion //for client side using jquery public JsonResult CityList(int mCountry) { try { if (mCountry != 0) { //below code is only for demo, you can pick city from database int index = 1; List lstCity = new List { new City { Country = 0, ID=null, Name = "Select" }, new City { Country = 1, ID=index++, Name = "Delhi" }, new City { Country = 1, ID=index++, Name = "Lucknow" }, new City { Country = 1, ID=index++, Name = "Noida" }, new City { Country = 1, ID=index++, Name = "Guragon" }, new City { Country = 1, ID=index++, Name = "Pune" }, new City { Country = 2, ID=index++, Name = "New-York" }, new City { Country = 2, ID=index++, Name = "California" }, new City { Country = 2, ID=index++, Name = "Washington" }, new City { Country = 2, ID=index++, Name = "Vermont" }, }; var city = from c in lstCity where c.Country == mCountry || c.Country == 0 select c; return Json(new SelectList(city.ToArray(), "ID", "Name"), JsonRequestBehavior.AllowGet); } } catch (Exception ex) { } return Json(null); } public ActionResult Index() { return View(); } public ActionResult CustomDropdown() { BindCountry(); BindCity(0); return View(); } [HttpPost] public ActionResult CustomDropdown(RegistrationModel mRegister) { if (ModelState.IsValid) { return View("Completed"); } else { ViewBag.SelCountry = mRegister.Country; BindCountry(); ViewBag.SelCity = mRegister.City; if (mRegister.Country != null) BindCity(mRegister.Country.ID); else BindCity(null); return View(); } } } }
How it works..
It's time to run the project and let's see the result as shown below :
What do you think?
I hope you will enjoy the tricks while programming with MVC Razor. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.
Комментариев нет:
Отправить комментарий