How to Insert data Using Ajax in mvc entity framework

Can anyone please help me how to solve this problem. 
Last updated:10/15/2015 4:07:37 AM

1 Answers

Anonymous User
Anonymous User

1- index Page

@model EntitiesProject.UserMaster

@using (Ajax.BeginForm(new AjaxOptions { HttpMethod = 'POST'}))

{     

    <div class='form-horizontal'>

    <label class='control-label col-sm-3' for='email'>User Name:</label>

    <div class='form-group'>

    <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' })

  </div>

   </div>

   <div class='form-group'>

    <label class='control-labelcol-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' })

  </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>

}

 

2- Usermaster class

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; }

    }

}

 

3- Home Controller 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();  

}

}

}

4-Layout

Answer