Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MVC3
how we can insert data in database using mvc3?
Posted 17-Jul-12 3:24am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
[HttpGet]
        public ActionResult addmovie()
        {
            return View();
        }
[HttpPost]
        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);
            db.Movies.AddObject(newmv);
            db.SaveChanges();
            return View();
        }
Step 5 : Create the view for actions we just created
@model MvcMovie.Models.movie
 
@{
    ViewBag.Title = "addmovie";
}
 
<h2>addmovie</h2>
 
@using (Html.BeginForm())
{
    
        <label>Title : </label>
        @Html.TextBoxFor(m=>m.title)
        @Html.ValidationMessageFor(m=>m.title)
    
    
        <label>Release DAte : </label>
        @Html.TextBoxFor(m=>m.releasedate)
        @Html.ValidationMessageFor(m=>m.releasedate)
    
    
        <label>Genre : </label>
        @Html.TextBoxFor(m=>m.genre)
        @Html.ValidationMessageFor(m=>m.genre)
    
    
        <label>Price : </label>
        @Html.TextBoxFor(m=>m.price)
        @Html.ValidationMessageFor(m=>m.price)
    
    
        <input id="Submit1" type="submit" value="Add Movie" />
    
}
  Permalink  
v2
Comments
9410662422 at 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 ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

hi
 

follow this
 
http://www.c-sharpcorner.com/UploadFile/e5b214/Asp-Net-mvc-3-database-connectivity-using-entity-framework/
  Permalink  
Comments
Member 10831242 at 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

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

  Print Answers RSS
0 Maciej Los 225
1 CHill60 170
2 Manfred R. Bihy 170
3 _Amy 155
4 Sergey Alexandrovich Kryukov 144
0 OriginalGriff 7,320
1 Sergey Alexandrovich Kryukov 6,043
2 Maciej Los 3,754
3 Peter Leow 3,448
4 CHill60 2,572


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 15 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100