Click here to Skip to main content
15,911,035 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi all

Iam new to MVC Pattern.I want to check a field exist or not.

C#
@Html.TextBoxFor(m => m.employeeid)
@Html.ValidationMessageFor(m => m.employeeid)


When i entered a employee code in textbox want to show msg as id exist if its already exist in database.

How to do this in mvc using any client side scripting ??

Thanks in Advance
Amritha
Posted
Updated 27-Feb-14 1:49am
v2
Comments
Sampath Lokuge 27-Feb-14 7:51am    
Are you using jQuery ?

1 solution

You can use jQuery ajax for this.

Note:Just a guidance.Change it according to your scenario.

JS

$.ajax({
              url: "/Portal/IsEmployeeExist",
              type: 'Get',
              dataType: "json",
              cache: false,
              data: { empId: passYourEmpId},
              success: function (data) {
                  if (!data) {
                      //your message
                  }
              },
              error: function (xhr, ajaxOptions, thrownError) {
                  //
              }
          });


Action method

C#
[HttpGet]
       public JsonResult IsEmployeeExist(string empId)
       {
           var isEmployeeExist = //here do your db validation and return true or false;

           return Json(isEmployeeExist, JsonRequestBehavior.AllowGet);
       }


Here is the nice Video about that : Evolving Practices in Using jQuery and Ajax in ASP.NET MVC Applications
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900