---
title: "How to use ajax validation in mvc entity framework."  
description: "How to use ajax validation in mvc entity framework."  
author: "Anonymous User"  
published: 2015-10-15  
updated: 2015-10-15  
canonical: https://www.mindstick.com/forum/33517/how-to-use-ajax-validation-in-mvc-entity-framework  
category: "asp.net mvc"  
tags: ["c#", "ajax", "jquery", "mvc4", "entity framework", "jquery validate"]  
reading_time: 3 minutes  

---

# How to use ajax validation in mvc entity framework.

Can [anyone](https://www.mindstick.com/articles/23207/how-to-find-the-best-fit-job-for-anyone) please help me how to [solve this problem](https://answers.mindstick.com/qa/94681/my-google-assistant-shows-completely-different-search-results-from-what-i-ask-how-can-i-solve-this-problem).

## Replies

### Reply by Anonymous User

```

```

```
@model
EntitiesProject.UserMaster
 <link href="/Content/bootstrap.min.css" rel="stylesheet" />
<script src="/Scripts/jquery-2.1.4.min.js"></script>
  <script src="/Scripts/bootstrap.min.js"></script> 
<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>
    <script src="/Scripts/jquery.unobtrusive-ajax.min.js"></script>   @using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "POST}))
{
   if (Model != null
    && Model.UserId > 0)
    {
    @Html.HiddenFor(x=>x.UserId)
    }
    <div class="form-horizontal">
        @Html.ValidationSummary()
        <div class="form-group">
       <label class="control-label col-sm-3" for="email">User Name:</label>
       <div class="col-sm-9">
        @Html.TextBoxFor(m => m.UserName, new { Class = "form-control",id = "name",        placeholder ="Enter Name" })
         @Html.ValidationMessageFor(model => model.UserName, "", new { Class = "text-danger" })            </div>
        </div>        <div class="form-group">        <label class="control-label col-sm-3" for="pwd">EmailId :</label>
        <div class="col-sm-9">
        @Html.TextBoxFor(m => m.EmailId, new { Class = "form-control",id = "emailid",
        placeholder = "Enter EmailId" })
        @Html.ValidationMessageFor(model => model.EmailId,"",new { Class = "text-danger" })
       </div>
        </div>
        <div class="form-group">
        <label class="control-label col-sm-3" for="phone">Phone Number:</label>
        <div class="col-sm-9">
        @Html.TextBoxFor(m => m.PhoneNumber, new { Class = "form-control",
        id = "phone",placeholder= "Enter Phone Number" })
         @Html.ValidationMessageFor(model => model.PhoneNumber, "", new { Class = "text-danger" })
         </div>
        </div>
        <div class="modal-footer">
         <button type="submit" class="btn btn-default">Save</button>
        <button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
        </div>
    </div>
}
```

```
namespace EntitiesProject
{
    using System;
    using System.Collections.Generic;
    public partial class UserMaster
    {
        public int UserId { get; set; }
        public string UserName { get; set; }
        public string PhoneNumber { get; set; }
        public string EmailId { get; set; }
    }
} 

namespace EntitiesProject
{
    public class UserMetaData
    {
        [Display(Name = "Name")]
        [Required(ErrorMessage = "Please enter your Name")]
        public string UserName { get; set; }
     [Display(Name = "Phone")]
    [StringLength(20, ErrorMessage = "The field Phone must be a char with a maximum length of 20.")]
    [RegularExpression(@"^([0]|\+91[\-\s]?)?[789]\d{9}$", ErrorMessage = "Entered Mobile No
     is not valid.")]
     [Required(ErrorMessage = "Please enter your Phone Number")]
     public string PhoneNumber { get; set; }
         [Required(ErrorMessage = "Please enter your email address")]
        [DataType(DataType.EmailAddress)]
        [Display(Name = "Email address")]
        [StringLength(50)]
        [RegularExpression(@"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Please
         enter correct email")]
    public string EmailId { get; set; }
    }
     [MetadataType(typeof(UserMetaData))]
    public partial class UserMaster
    {
    }
  }
namespace EntitiesProject.Controllers
{
    public class HomeController : Controller
    {       [HttpPost]
        public ActionResult Add(UserMaster data)
        {
            if (ModelState.IsValid)
            {
                db.UserMasters.Add(data);
                db.SaveChanges();
            }

            return new EmptyResult();
        }
}
}
```


---

Original Source: https://www.mindstick.com/forum/33517/how-to-use-ajax-validation-in-mvc-entity-framework

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
