The function implementation code of chapter 4 of the MVC4 website tutorial, and chapter 4 of mvc4

Source: Internet
Author: User

The function implementation code of chapter 4 of the MVC4 website tutorial, and chapter 4 of mvc4

Collation
I. Users
Ii. User Group
Iii. Topic
3.1 Add a topic
3.2 browse topic
3.3 update topic
3.4 delete a topic
3.5 foreground topic browsing
......
Topic model;

The model should have the following fields: column name, parent column id, column type, content model, column view, content view, link address, and column sorting. For the time being, I think so much about it first.

The topic name and parent topic id are not to mention easy.

There are three types of columns: Common columns-General columns; single-page columns-a column is a page, such as a company introduction or contact address; external link: click a column to jump to a link.

Content Model-refers to the content of a common topic, such as news, articles, and messages.

Topic view-is the view name used by the topic. It is invalid when the topic type is Link.

Content view: it refers to the view of the specific content of a topic. When the topic model is news, the view used to open news under the topic is only valid when the topic type is normal.

Link address-click the address to jump to, which is valid when the column type is Link.

Column sorting-the basis for column sorting. The smaller the number of columns at the same level, the higher the ranking.

Right-click the Models folder and choose "add Category ".

Using System. componentModel. dataAnnotations; namespace Ninesky. models {/// <summary> /// topic model /// </summary> public class Category {[Key] public int CategoryId {get; set ;} /// <summary> /// topic Name /// </summary> [Display (Name = "topic Name", Description = "2-20 characters")] [Required (ErrorMessage = "×")] [StringLength (20, MinimumLength = 2, ErrorMessage = "×")] public string Name {get; set ;} /// <summary> /// parent column No. /// </ Summary> [Display (Name = "parent")] [Required (ErrorMessage = "×")] public int ParentId {get; set ;} /// <summary> /// topic type [0-common topic; 1-single-page topic; 2-external link] /// </summary> [Display (Name = "topic Type")] [Required (ErrorMessage = "×")] public int Type {get; set ;} /// <summary> /// Content Model [effective only when the column is normal] /// </summary> [Display (Name = "Content Model")] public string Model {get; set ;}/// <summary> /// topic view /// </summary> [Display (Name = "Topic View", Description = "topic page view, which can contain a maximum of 255 characters .. ")] [StringLength (255, ErrorMessage =" × ")] public string CategoryView {get; set ;} /// <summary> /// content page view /// </summary> [Display (Name = "content View", Description = "Content Page View, it can contain a maximum of 255 characters .. ")] [StringLength (255, ErrorMessage =" × ")] public string ContentView {get; set ;} /// <summary> /// link address /// </summary> [Display (Name = "link address", Description = "link address to jump to when you click a column, it can contain a maximum of 255 characters. ")] [StringLength (255, ErrorMessage =" × ")] public string Navigation {get; set ;} /// <summary> /// column sorting /// </summary> [Display (Name = "column sorting", Description = "the smaller the number, the higher the order. ")] Public int Order {get; set ;}}}

Add column type enumeration under the Category class

Public enum CategoryType {general topic, single-page topic, external link}

Open NineskyContext. cs and add the public DbSet <Category> Categorys {get; set;} code above. The file is as follows :.

using Ninesky.Models;using System.Data.Entity;namespace Ninesky.Repository{  public class NineskyContext:DbContext  {    public DbSet<User> Users { get; set; }    public DbSet<UserGroup> UserGroups { get; set; }    public DbSet<Category> Categorys { get; set; }    public NineskyContext()      : base()    {      Database.CreateIfNotExists();    }  }}

Add the CategoryRepository class to the Repository folder. This class inherits from the RepositoryBase <Category> class, and write functions such as add, delete, modify, and search in the class. Complete the following steps:

Using System; using System. collections. generic; using System. linq; using System. web; using Ninesky. models; namespace Ninesky. repository {public class CategoryRepository: repositoryBase <Category >{/// <summary> /// Add a topic /// </summary> /// <param name = "category"> topic </param>/ // <returns> </returns> public override bool Add (Category category) {dbContext. categorys. add (category); if (dbContext. saveChanges ()> 0) return true; else return false ;} /// <summary> /// update the topic /// </summary> /// <param name = "category"> topic </param> /// <returns> </returns> public override bool Update (Category category) {dbContext. categorys. attach (category); dbContext. entry <Category> (category ). state = System. data. entityState. modified; if (dbContext. saveChanges ()> 0) return true; else return false ;} /// <summary> /// delete a topic /// </summary> /// <param name = "category"> topic </param> /// <returns> </returns> public bool Delete (Category category) {dbContext. categorys. remove (category); if (dbContext. saveChanges ()> 0) return true; else return false ;} /// <summary> /// delete a topic /// </summary> /// <param name = "CategoryId"> topic Id </param> /// <returns> </returns> public override bool Delete (int CategoryId) {var _ category = dbContext. categorys. singleOrDefault (c => c. categoryId = CategoryId); if (_ category = null) return false; else return Delete (_ category );} /// <summary> /// search for a specific topic /// </summary> /// <param name = "CategoryId"> topic id </param> /// <returns> </returns> public override Category Find (int CategoryId) {return dbContext. categorys. singleOrDefault (c => c. categoryId = CategoryId );} /// <summary> /// obtain the followed topic /// </summary> /// <returns> </returns> public IQueryable <Category> Root () {return Children (0 );} /// <summary> /// obtain the subtopic /// </summary> /// <param name = "CategoryId"> topic Id </param> /// <returns> </returns> public IQueryable <Category> Children (int CategoryId) {return dbContext. categorys. where (c => c. parentId = CategoryId ). orderBy (c => c. order );} /// <summary> /// column list /// </summary> /// <param name = "model"> model name </param> /// <returns> </returns> public IQueryable <Category> List (string model) {return dbContext. categorys. where (c => c. model = model ). orderBy (c => c. order );}}}

This is the time for preparation. OK!

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.