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" />
}