ASP. net mvc learning Summary (2)

Source: Internet
Author: User

Not long ago, I learned the webform of asp.net before learning MVC. I felt that thinking had changed a lot and I couldn't help using webform, so I forgot something about webform, start from scratch to understand MVC3.0. Below is a simple start.

In asp.net's webform, if you want to write, you will write a lot of SQL statements, such as INSERT INTO TABLE (), SELECT ..... In short, it is necessary to write a large number of SQL statements, especially in complex paging SQL statements. If the statements are not well written, there will be some inintention errors, let's take a look at how to add, delete, query, and modify MVC3.0:

(1) Add data with the following code:

 1   public ActionResult Create(User dpt) 2         { 3             var data = db.Departments.OrderByDescending(p => p.Name); 4             ViewBag.DptName = data; 5             try 6             { 7                 db.Entry(dpt).State = System.Data.EntityState.Added; 8                 db.SaveChanges(); 9             }10             catch (Exception ex)11             {12                 ViewBag.Ex = ex.Message;13             }14 15             return View();16         }

Let's take a look at the colored code in the code. It's simple enough. It's so simple. Do you want to write SQL statements after reading it? Are you still writing a lot of "redundant code" to get data from the foreground? Try it!

(2) Let's take a look at the deleted code. You don't need to write such as DELETE .. The Code is as follows:

1   public ActionResult Delete(string LoginName)2         {3             User users = new User();4             users.LoginName = LoginName;5             db.Entry(users).State = System.Data.EntityState.Deleted;6             db.SaveChanges();7             return RedirectToAction("Index", new { id = Request.QueryString["page"] });8         }

(3) The data list Code is as follows:

 1  public ActionResult Index(int? id) 2         { 3             var list = from p in db.users.Include(p => p.Department) orderby p.ID descending select p; 4             IEnumerable<User> data = list.ToPagedList(id ?? 1, 5); 5             if (Request.IsAjaxRequest()) 6             { 7                 return PartialView("_Index", data); 8             } 9             else10             {11                 return View("Index", data);12             }13         }

How about it? Are you still worrying about complex code like paging SQL for data on the list page? Don't hesitate. Please try it, but ToPagedList (id ?? 1, 5); It is implemented. It is not included in the actual MVC3.0. Next time, we will provide a method that does not use the implemented code, summary .........

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.