1 Answers

Aditya Patel
Aditya Patel


@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();

        }

}

}

Answer