Home > DeveloperSection > Forums > How to Insert data Using Ajax in mvc entity framework
Ankit Singh

Total Post:341

Points:2389
Posted on    October-15-2015 3:25 AM

 ASP.NET MVC C#  AJAX  Mvc4  Entity Framework  Ajaxform 
Ratings:


 1 Reply(s)
 670  View(s)
Rate this:
Can anyone please help me how to solve this problem. 


aditya kumar Patel

Total Post:246

Points:1752
Posted on    October-15-2015 4:07 AM

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


Don't want to miss updates? Please click the below button!

Follow MindStick