HOW TO INSERT DATA USING AJAX IN MVC ENTITY FRAMEWORK

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
 2119  View(s)
Ratings:
Rate this:
Can anyone please help me how to solve this problem. 
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: How to Insert data Using Ajax in mvc entity framework

    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

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!