Introduction
In this article I am going to explain about "Remote Validation" using MVC 5.Remote validation is mainly used to check user or mailid exist or not in database ,also you can use for any type of validation using Remote Attribute.
Most of web application needs to check user or mailid exist or not in database ,in that developers needs to write huge amount of code,that's why i am going to achieve this task in very simple steps .Remote validation is normally works as jquery validation.
Before explain about "Remote Validation" ,if you are beginner in MVC that's why i am listing below some important links.
What is Remote validation
Remote validation permits the developer to call the controller action using client side script. This is to a great degree valuable when you need to perform out a back end query without performing a full server side postback.
Implement of "Remote Validation" in MVC Application.
Remote validation is only an Ajax call which approves the client's information. This can undoubtedly be proficient without using ASP.NET MVC 5. The situation for our demo is very basic. We have to ensure that the EmpNameof the new user is one of a kind. The execution beneath makes a basic structure which contains the TextBox control with the id EmpName" and other TextBox control with Id EmpMail.
Follow some simple steps to use remote validation in MVC 5
Step 1:- Create a MVC application.see image...
Step 2:-Create a model "Employee.cs" inside "Model" folder.use this code.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace RemoteValidation.Models
- {
- public class Employee
- {
- //To Check userName
- [Remote("IsEmpNameExist", "Validation", ErrorMessage = "Employee name already exist")]
- public string EmpName
- {
- get;
- set;
- }
- //check User name and mailId.
- [Remote("IsEmpNameandMailExist", "Validation", ErrorMessage = "EmialId is already exist", AdditionalFields = "EmpName")]
- public string EmpMail
- {
- get;
- set;
- }
- }
- //Set static value in model.
- public static class EmployeeStaticData
- {
- public static List<Employee> UserList
- {
- get
- {
- return new List<Employee>
- {
- new Employee
- {
- EmpName = "Bikesh", EmpMail = "Bikesh.srivastava@hytechpro.com"
- },
- new Employee
- {
- EmpName = "Navdeep", EmpMail = "Navdeep.kumar@hytechpro.com"
- }
- };
- }
- }
- }
- }
- using RemoteValidation.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace RemoteValidation.Controllers
- {
- public class ValidationController : Controller
- {
- //For check both at a time .mailid and empName.
- [HttpGet]
- public JsonResult IsEmpNameandMailExist(string Empname, string EmpMail)
- {
- bool isExist = EmployeeStaticData.UserList.Where(u=>u.EmpName.ToLowerInvariant().Equals(Empname.ToLower()) && u.EmpMail.ToLowerInvariant().Equals(EmpMail.ToLower())).FirstOrDefault() != null;
- return Json(!isExist, JsonRequestBehavior.AllowGet);
- }
- //To check only EmpName
- [HttpGet]
- public JsonResult IsEmpNameExist(string Empname)
- {
- bool isExist = EmployeeStaticData.UserList.Where(u => u.EmpName.ToLowerInvariant().Equals(Empname.ToLower()))!= null;
- return Json(!isExist, JsonRequestBehavior.AllowGet);
- }
- }
- }
Step 4-Create controller to execute your action ,In this controller you can create any type of action to perform your task.use this code .
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using RemoteValidation.Models;
- namespace RemoteValidation.Controllers
- {
- public class EmployeeController : Controller
- {
- //my action
- [HttpGet]
- public ActionResult AddUser()
- {
- Employee model = new Employee();
- return View(model);
- }
- }
- }
Step 5-Right click on Action name "AddUser" add view. In this view i am binding model "Employee" .using Razor control i am creating a login page which is designed by bootstrap and css,jquery. use this code.
- @model RemoteValidation.Models.Employee
- <div class="panel panel-primary">
- <div class="panel-heading panel-head"> Add User</div>
- <div class="panel-body">
- @using (Html.BeginForm())
- {
- <div class="form-horizontal">
- <div class="form-group">
- @Html.LabelFor(model => model.EmpName, new { @class = "col-lg-2 control-label" })
- <div class="col-lg-9">
- @Html.TextBoxFor(model => model.EmpName, new { @class = "form-control" })
- @Html.ValidationMessageFor(model => model.EmpName)
- </div> </div>
- <div class="form-group">
- @Html.LabelFor(model => model.EmpMail, new { @class = "col-lg-2 control-label" })
- <div class="col-lg-9">
- @Html.TextBoxFor(model => model.EmpMail, new { @class = "form-control" })
- @Html.ValidationMessageFor(model => model.EmpMail)
- </div>
- </div>
- <div class="form-group">
- <div class="col-lg-9"></div> <div class="col-lg-3">
- <button class="btn btn-success"
- id="btnSubmit" type="submit">
- Submit
- </button>
- </div>
- </div>
- </div>
- }
- </div>
- </div>
- @section scripts
- {
- @Scripts.Render("~/bundles/jqueryval")
- }
Step 6:-Don't forget this step.add these lines in "Web.config" file.use this code inside "<appseting>".
- <appSettings>
- <add key="ClientValidationEnabled" value="true" />
- <add key="UnobtrusiveJavaScriptEnabled" value="true" />
- </appSettings>
- using System.Web;
- using System.Web.Optimization;
- namespace RemoteValidation
- {
- public class BundleConfig
- {
- // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
- public static void RegisterBundles(BundleCollection bundles)
- {
- bundles.Add(new StyleBundle("~/Content/css").Include(
- "~/Content/css/bootstrap.css",
- "~/Content/css/font-awesome.css",
- "~/Content/css/site.css"));
- bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
- "~/Scripts/jquery-{version}.js"));
- bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
- "~/Scripts/jquery.validate*"));
- }
- }
- }
According this image you can see ,i am typing "Bikesh" ,showing error message "Employee name already exists" because i have written hard code "Bikesh" in model.
There are two situation to check validation.
1.you can check only Employee Name
2.you can check both condition "Employee Name" and Email Id.
I hope you understand all things about "Remote validation".if any problem you can download project and run on your system.
You have just read an article that categorized by title MVC
by title Remote validation in MVC 5,To Check UserName and MailId is Already exist or not.. You can bookmark this page with a URL https://bikeshsrivastava.blogspot.com/2016/09/part-37remote-validation-in-mvc-5to.html. Thank You!
Author:
Bikesh Srivastava - Wednesday, September 28, 2016
keep update the blog,it was a nice information regarding.....
ReplyDeleteAngular 5 training
Angular 5 training in Hyderabad
Angular 5 online training in Hyderabad
Best Angular 5 training institutes in Hyderabad