Controller code:
Public classHomecontroller:controller {PrivateNewdbcontext NDC =NewNewdbcontext (); PublicActionResult Index (stringsearch) {List<Movie> B =ndc.movies.ToList (); if(string. IsNullOrEmpty (search)) B.where (x= = X.title = =search). ToList (); viewdata["Title"] = B.select (A =NewSelectListItem {Value = a.title, Text =A.title}). ToList (); returnView (b); } [HttpPost] Public stringIndex (stringsearch) { return "This post"+search; } PublicActionResult Create () {returnView (); } [HttpPost] [Validateantiforgerytoken] PublicActionResult Create (Movie b) {if(modelstate.isvalid) {ndc.movies.Add (b); Ndc. SaveChanges (); } returnRedirecttoaction ("Index"); } PublicActionResult Edit (intid) {Movie m=ndc.movies.Find (ID); if(M = =NULL) returnHttpnotfound (); returnView (m); } [HttpPost] [Validateantiforgerytoken] Publicactionresult Edit (Movie m) {if(modelstate.isvalid) {NDC. Entry (M). State=entitystate.modified; Ndc. SaveChanges (); returnRedirecttoaction ("Index"); } returnView (m); } PublicActionResult Details (intid) {Movie m=ndc.movies.Find (ID); if(M = =NULL)returnHttpnotfound (); returnView (m); } PublicActionResult Delete (intid) {Movie m=ndc.movies.Find (ID); if(M = =NULL)returnHttpnotfound (); Ndc.movies.Remove (m); Ndc. SaveChanges (); returnRedirecttoaction ("Index"); } }
View Code
Model Code
Public classMovie { Public intId {Get;Set; } [Required] [Display (Name="title")] [Stringlength ( -, Minimumlength =1)] Public stringTitle {Get;Set; } [Required] [Display (Name="Time")] [DataType (datatype.date)] [DisplayFormat (dataformatstring="{0:YYYY-MM-DD}", Applyformatineditmode =true)] PublicDateTime ReleaseDate {Get;Set; } [Required] [Display (Name="author")] Public stringGenre {Get;Set; } [Display (Name="Price")] [Range (1, -)] [DataType (datatype.currency)] Public decimalPrice {Get;Set; } } Public classNewdbcontext:dbcontext { Publicdbset<movie> Movies {Get;Set; } }
View Code
---------------------------------------------
The following is the data lookup function
Getting Started with EF (movie management)