Click here to Skip to main content
15,892,072 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?
Posted

hi

follow this

http://www.c-sharpcorner.com/UploadFile/e5b214/Asp-Net-mvc-3-database-connectivity-using-entity-framework/
 
Share this answer
 
Comments
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
C#
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
C#
[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
HTML
@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" />
    
}
 
Share this answer
 
v2
Comments
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