Click here to Skip to main content
15,122,149 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how we can insert data in database using mvc3?


follow this
Member 10831242 28-May-14 3:58am
i have created contextclass and inseting record and retrieving also..but i dont konw where that record stores...where is the DB now ??? how can i check
Step 1 : Add ADO.NET entity data model for your table, it will be added in model folder
Step 2 : Create a class for the table in which you are going to insert data in model folder
public class movie
        public string title { get; set; }
        public DateTime releasedate { get; set; }
        public string genre { get; set; }
        public decimal price { get; set; }

Step 3 : Create a controller in which you are going to put code for insert method
Step 4 : Create two actions in controller file, one for HttpGet and HttpPost
        public ActionResult addmovie()
            return View();
        public ActionResult addmovie(movie mv)
            Movie newmv = new Movie();
            newmv.Title = mv.title;
            newmv.ReleaseDate = mv.releasedate;
            newmv.Genre = mv.genre;
            newmv.Price = Convert.ToDecimal(mv.price);
            return View();

Step 5 : Create the view for actions we just created

    ViewBag.Title = "addmovie";


@using (Html.BeginForm())
        <label>Title : </label>
        <label>Release DAte : </label>
        <label>Genre : </label>
        <label>Price : </label>
        <input id="Submit1" type="submit" value="Add Movie" />
Ashish Jain(Be Jovial) 1-Apr-13 7:14am
Where you have created your DataContext object ? I am asking because i have created the object of DataContext classes in my Model class. And if i talk about your code you have written "db.SaveChanges()" in your controller class. Please explain how you have accessed DataContext class object from model in controller OR Have you created the object in your controller ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900