The increase and deletion of ASP. NET MVC Ef-dbfirst

Source: Internet
Author: User

Controller and action method:

Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Mvc;using Mvc5test.models;using system.data.entity.infrastructure;namespace mvc5test.controllers{// The user's request is always first sent to an action method of the controller, and the corresponding view is returned by the action method public class Homecontroller:controller {//Data context object Bogent        Ities db = new bogentities (); Query article Public ActionResult Index () {//sqo standard query operator//dbquery<classtype> queries = d B.classtypes.where (ct = ct).            parentid==0) as dbquery<classtype>; List<classtype> ctlist = db. Classtype.where (ct = ct). ParentID = = 0).            Tolist<classtype> (); Linq list<artical> alist = (from a in db. artical where A.classtypeid! = 4 Select a).            ToList ();            Use ViewData to pass data to the view viewdata["alist"] = alist;        return View ();      }//delete article//the default parameter ID here App_start RouteConfig.cs config public actionresult Del (int id)  {try {//1. Create the object to be deleted artical A = new artical {id = id}; 2. Add the object to the EF Management container db.                Artical.attach (a); 3. Identify the state of the object wrapper class as Delete db.                Artical.remove (a); 4. Update Database DB.                SaveChanges ();            5. The update succeeds, the command browser jumps to the list page return redirecttoaction ("Index", "Home"); } catch (Exception e) {return Content ("Delete failed!!!            "+e.message);            }}//modify article-show action [HttpGet]//get request when executing the following method public ActionResult Edit (int id) { 1. Query the database by ID, return the first object in the collection artical a = (from at DB. artical where at.id = = ID select at).            FirstOrDefault (); 2. Generate the article Classification data drop-down list collection ienumerable<selectlistitem> selectlist = (from CT in db. ClassType Select CT). ToList ()//from DB.                ClassType the selection of elements in a collection to form a new set of CT                                      . Select (ct = new SelectListItem {Value = ct.id. ToString (), Text = Ct. ClassName}//Iterate through each element in the CT collection and turn each element that is traversed into a SelectListItem object).            ToList ();            Use ViewBag to pass data to view viewbag.ctlist = SelectList;        3. Pass the article to the view//*** use the view's construction method to pass the data to a property named model on the View Return view (a); }//Modify article-Save action [HttpPost]//post request when executing the following method public ActionResult Edit (artical model)//This time the model is the page to be modified The article entity object {try {//1. Add the Entity object to the EF object container and get the Pseudo wrapper class object Dbentityentry<a Rtical> entry = db.                Entry<artical> (model); 2. Set the state of the wrapper class object to unchanged entry.                state = System.Data.Entity.EntityState.Unchanged; 3. Set the changed attribute entry. Property (A = A.title).                IsModified = true; Entry. Property (A = A.classtypeid).                IsModified = true; //4. Commit to the database and complete the change db.                SaveChanges ();            5. The update succeeds, then the command browser redirects to the list page return redirecttoaction ("Index", "Home"); } catch (Exception e) {return Content ("Modify failed!!!            "+e.message);            }}//new article-Show action [httpget] public ActionResult Add () {//Generate article Category data drop-down list collection Ienumerable<selectlistitem> selectlist = (from CT in db. ClassType Select CT). ToList ()//from DB. The ClassType collection selects the elements to form a new set of Ct. Select (ct = new SelectListItem {Value = ct.id. ToString (), Text = Ct. ClassName}//Iterate through each element in the CT collection and turn each element that is traversed into a SelectListItem object).            ToList ();            Use ViewBag to pass data to view viewbag.ctlist = SelectList;        return View (); }//new article-Save action [HttpPost] public actionresult Add (artical moDel) {try {//1. Add the Entity object to the EF object container and get the Pseudo wrapper class object Dbentityentry<arti Cal> entry = db.                Entry<artical> (model); 2. Set the state of the wrapper class object to added entry.                state = System.Data.Entity.EntityState.Added; 3. Submit to the database to complete the new db.                SaveChanges ();            4. The update succeeds, then the command browser redirects to the list page return redirecttoaction ("Index", "Home"); } catch (Exception e) {return Content ("New failed!!!            "+ e.message); }        }    }}

First page Data list view:

@using mvc5test.models;@{Layout = null;} 

New View:

@model Mvc5Test.Models.Artical @{Layout = null;} <! DOCTYPE html>

Edit view:

@model Mvc5Test.Models.Artical <!--The above code is the type of the specified page model property-->@{Layout = null;} <! DOCTYPE html>

  

The increase and deletion of ASP. NET MVC Ef-dbfirst

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.