MVC4 website production tutorial chapter 4 part of the column function implementation code

  • 2021-08-12 02:32:09
  • OfStack

Sequence
1. Users
2. User groups
3. Column
3.1 Add Column
3.2 Browse columns
3.3 Update Column
3.4 Delete Column
3.5 Front Desk Column Browsing
...
Column model;

The model should have 1 following fields: Column Name, Parent Column id, Column Type, Content Model, Column View, Content View, Link Address, Column Sorting. Think of so much for the time being, and it says here first.

The column name and parent column id are needless to say very simple.

There are three types of columns: ordinary columns-that is, 1-like columns; Single-page column-refers to a column is a page, such as company introduction, contact address, etc.; External link-refers to the column point 1 and jumps to 1 link.

Content model-refers to the content of a column when the column is an ordinary column, such as news, articles, messages, etc.

Column View-is the view name used by the column and is invalid when the column type is link.

Content View-A view of the specific content of a column, such as the view used when opening news under a column when the column model is news, which is only valid when the column type is ordinary column.

Link Address-The address to which you click to jump, which is valid when the column type is link.

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

After the field is confirmed, right-click in the Models folder to add the Category class


using System.ComponentModel.DataAnnotations;

namespace Ninesky.Models
{
  /// <summary>
  ///  Column model 
  /// </summary>
  public class Category
  {
    [Key]
    public int CategoryId { get; set; }
    /// <summary>
    ///  Column name 
    /// </summary>
    [Display(Name=" Column name ",Description="2-20 Characters ")]
    [Required(ErrorMessage=" × ")]
    [StringLength(20,MinimumLength=2,ErrorMessage=" × ")]
    public string Name { get; set; }
    /// <summary>
    ///  Parent column number 
    /// </summary>
    [Display(Name=" Parent column ")]
    [Required(ErrorMessage=" × ")]
    public int ParentId { get; set; }
    /// <summary>
    ///  Column type " 0- Ordinary columns; 1- Single page column; 2- External links " 
    /// </summary>
    [Display(Name=" Column type ")]
    [Required(ErrorMessage = " × ")]
    public int Type { get; set; }
    /// <summary>
    ///  Content model "valid only when the column is a normal column" 
    /// </summary>
    [Display(Name=" Content model ")]
    public string Model { get; set; }
    /// <summary>
    ///  Column view 
    /// </summary>
    [Display(Name = " Column view ", Description = " The view of the column page, up to 255 Characters. . ")]
    [StringLength(255, ErrorMessage = " × ")]
    public string CategoryView { get; set; }
    /// <summary>
    ///  Content page view 
    /// </summary>
    [Display(Name = " Content view ", Description = " Content page view, up to 255 Characters. . ")]
    [StringLength(255, ErrorMessage = " × ")]
    public string ContentView { get; set; }
    /// <summary>
    ///  Link address 
    /// </summary>
    [Display(Name=" Link address ",Description=" The link address to jump to when clicking the column , Maximum 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 Category class


 public enum CategoryType
  {
    1 General column ,  Single-page column ,  External link 
  } 

Open NineskyContext. cs Add public DbSet above < Category > Categorys {get; set;} This one sentence code, after the completion of the file 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 in the Repository folder, which inherits from RepositoryBase < Category > Write add, delete, modify, find and other functions in the class. Complete as follows:


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 Column 
    /// </summary>
    /// <param name="category"> Column </param>
    /// <returns></returns>
    public override bool Add(Category category)
    {
      dbContext.Categorys.Add(category);
      if (dbContext.SaveChanges() > 0) return true;
      else return false;
    }
    /// <summary>
    ///  Update column 
    /// </summary>
    /// <param name="category"> Column </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 Column 
    /// </summary>
    /// <param name="category"> Column </param>
    /// <returns></returns>
    public bool Delete(Category category)
    {
      dbContext.Categorys.Remove(category);
      if (dbContext.SaveChanges() > 0) return true;
      else return false;
    }
    /// <summary>
    ///  Delete Column 
    /// </summary>
    /// <param name="CategoryId"> Column 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>
    ///  Find and formulate columns 
    /// </summary>
    /// <param name="CategoryId"> Column id</param>
    /// <returns></returns>
    public override Category Find(int CategoryId)
    {
      return dbContext.Categorys.SingleOrDefault(c => c.CategoryId == CategoryId);
    }
    /// <summary>
    ///  Get a follow-up column 
    /// </summary>
    /// <returns></returns>
    public IQueryable<Category> Root()
    {
      return Children(0);
    }
    /// <summary>
    ///  Get subcolumns 
    /// </summary>
    /// <param name="CategoryId"> Column 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);
    }
  }
}

That's the preparation, OK!


Related articles: