Getting Started with EF (movie management)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.