篇(18)-Asp.Net Core入门实战-文章管理之文章内容管理(下拉框二级结构递归)

篇(18)-Asp.Net Core入门实战-文章管理之文章内容管理(下拉框二级结构递归实现)

文章管理是CMS系统的核心表之一,存储文章内容,特点就是字段端,属性多,比如是否标识为热点、推荐等属性,是否发布,类别,SEO关键字等。我们本章讲解文章内容的增删改查。

(1).文章Sql表结构设计

篇(18)-Asp.Net Core入门实战-文章管理之文章内容管理(下拉框二级结构递归)插图

CREATE TABLE [dbo].[Article](
[Id] [int] IDENTITY(1,1) NOT NULL,
[CategoryId] [int] NOT NULL,
[Title] [varchar](128) NOT NULL,
[ImageUrl] [varchar](128) NULL,
[Content] [text] NULL,
[ViewCount] [int] NOT NULL,
[Sort] [int] NOT NULL,
[Author] [varchar](64) NULL,
[Source] [varchar](128) NULL,
[SeoTitle] [varchar](128) NULL,
[SeoKeyword] [varchar](256) NULL,
[SeoDescription] [varchar](512) NULL,
[AddManagerId] [int] NOT NULL,
[AddTime] [datetime] NOT NULL,
[ModifyManagerId] [int] NULL,
[ModifyTime] [datetime] NULL,
[IsTop] [bit] NOT NULL,
[IsSlide] [bit] NOT NULL,
[IsRed] [bit] NOT NULL,
[IsPublish] [bit] NOT NULL,
[IsDeleted] [bit] NOT NULL,
CONSTRAINT [PK_ARTICLE] PRIMARY KEY NONCLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[Article] ADD DEFAULT (getdate()) FOR [AddTime]
GO
ALTER TABLE [dbo].[Article] ADD DEFAULT ((0)) FOR [IsTop]
GO
ALTER TABLE [dbo].[Article] ADD DEFAULT ((0)) FOR [IsSlide]
GO
ALTER TABLE [dbo].[Article] ADD DEFAULT ((0)) FOR [IsRed]
GO
ALTER TABLE [dbo].[Article] ADD DEFAULT ((0)) FOR [IsPublish]
GO
ALTER TABLE [dbo].[Article] ADD DEFAULT ((0)) FOR [IsDeleted]
GO
ALTER TABLE [dbo].[Article] WITH CHECK ADD CONSTRAINT [FK_ARTICLE_RELATIONS_ARTICLEC] FOREIGN KEY([CategoryId])
REFERENCES [dbo].[ArticleCategory] ([Id])
GO
ALTER TABLE [dbo].[Article] CHECK CONSTRAINT [FK_ARTICLE_RELATIONS_ARTICLEC]
GO

 

那么对应的Article Model代码如下:

public class Article
{
/// 
/// 主键
/// 
[Key]
public Int32 Id { get; set; }
/// 
/// 分类ID
/// 
[Required]
public Int32 CategoryId { get; set; }
/// 
/// 文章标题
/// 
[Required]
public String Title { get; set; }
/// 
/// 图片地址
/// 
public String ImageUrl { get; set; }
/// 
/// 文章内容
/// 
public String Content { get; set; }
/// 
/// 浏览次数
/// 
[Required]
public Int32 ViewCount { get; set; }
/// 
/// 排序
/// 
[Required]
public Int32 Sort { get; set; }
/// 
/// 作者
/// 
public String Author { get; set; }
/// 
/// 来源
/// 
public String Source { get; set; }
/// 
/// SEO标题
/// 
public String SeoTitle { get; set; }
/// 
/// SEO关键字
/// 
public String SeoKeyword { get; set; }
/// 
/// SEO描述
/// 
public String SeoDescription { get; set; }
/// 
/// 添加人ID
/// 
[Required]
public Int32 AddManagerId { get; set; }
/// 
/// 添加时间
/// 
[Required]
public DateTime AddTime { get; set; }
/// 
/// 修改人ID
/// 
public Int32? ModifyManagerId { get; set; }
/// 
/// 修改时间
/// 
public DateTime? ModifyTime { get; set; }
/// 
/// 是否置顶
/// 
public Boolean IsTop { get; set; }
/// 
/// 是否轮播显示
/// 
public Boolean IsSlide { get; set; }
/// 
/// 是否热门
/// 
public Boolean IsRed { get; set; }
/// 
/// 是否发布
/// 
public Boolean IsPublish { get; set; }
/// 
/// 是否删除
/// 
[Required]
public Boolean IsDeleted { get; set; }
}

 

(2).视图Create代码

(2.1)视图代码

考虑到要同时上传图片,注意form表单的额 enctype类型;

@{ ViewData["Title"] = "新建文章"; }
@model Article
form action="/Article/Create" method="post" enctype="multipart/form-data">
@Html.AntiForgeryToken()
div>
label asp-for="Title">标题label>
div>
input type="text" asp-for="Title" name="Title" placeholder="请输入标题">
div>
div>
div>
label asp-for="CategoryId">文章类型label>
div>
@Html.DropDownList("ddl_CategoryId", ViewBag.database as IEnumerableSelectListItem>)
div>
div>
div>
label>设置label>
div>
@*@Html.CheckBox("IsTop") 置顶
@Html.CheckBox("IsRed") 热点
@Html.CheckBox("IsSlide") 幻灯*@
input type="checkbox" name="IsTop" asp-for="IsTop" />置顶
input type="checkbox" name="IsRed" asp-for="IsRed"/>热点
input type="checkbox" name="IsSlide" asp-for="IsSlide" />幻灯
div>
div>
div>
label asp-for="ImageUrl">文章首页图label>
div>
input type="file" asp-for="ImageUrl" name="ImageUrl"/>
div>
div>
div>
label asp-for="Content">内容label>
div>
textarea placeholder="内容" asp-for="Content" name="Content" cols="30" rows="10">textarea>
div>
div>
div>
label asp-for="Sort">排序label>
div>
input type="text" placeholder="排序" asp-for="Sort" name="Sort" />
div>
div>
div>
label asp-for="ViewCount">点击量label>
div>
input type="text" placeholder="点击量" asp-for="ViewCount" name="ViewCount" />
div>
div>
div>
label asp-for="IsPublish">是否发布label>
div>
select asp-for="IsPublish" name="IsPublish" class="IsPublish">
option value="False">option>
option value="True" selected>option>
select>
div>
div>
div>
label asp-for="Author">作者label>
div>
input type="text" asp-for="Author" name="Author" placeholder="作者名">
div>
div>
div>
label asp-for="Source">来源label>
div>
input type="text" asp-for="Source" name="Source" placeholder="文章来源">
div>
div>
div>
label asp-for="SeoTitle">SEO标题label>
div>
input type="text" asp-for="SeoTitle" name="SeoTitle" placeholder="SEO标题">
div>
div>
div>
label asp-for="SeoKeyword">SEO关键词label>
div>
input type="text" asp-for="SeoKeyword" name="SeoKeyword" placeholder="SEO关键词">
div>
div>
div>
label asp-for="SeoDescription">SEO摘要描述label>
div>
input type="text" asp-for="SeoDescription" name="SeoDescription" placeholder="SEO摘要描述">
div>
div>
div>
div>
button type="submit">确定button>
button type="reset">重置button>
div>
div>
form>

 

(2.2)视图中的下拉框的实现方式(递归和循环嵌套)

篇(18)-Asp.Net Core入门实战-文章管理之文章内容管理(下拉框二级结构递归)插图1

我想在添加文章时,实现一个具有二级层次结构的下拉框,如上图所示。所以,在对下拉框进行数据绑定时,就要费点功夫,上个章节讲文章类别管理时,的表结构就一个,分类都存在一张表中,所以要进行递归的获取子菜单或者通过循环嵌套来实现。

递归的主要核心函数为:

/// 
/// 递归函数,实现获取子菜单
/// 
/// 递归前的列表
/// 递归后的新列表
/// 父Id
/// 
public static List GetChildCategory(List lists, List newlists, int parentId)
{
newlists = new List();
List tempList = lists.Where(c => c.ParentId == parentId).ToList();
for (int i = 0; i )
{
CategorySelectItemListView category = new CategorySelectItemListView();
category.Id = tempList[i].Id;
category.ParentId = tempList[i].ParentId;
category.Title = tempList[i].Title;
category.Children = GetChildCategory(lists, newlists, category.Id);
newlists.Add(category);
}
return newlists;
}

/// 
/// 循环嵌套,实现获取子菜单
/// 
/// 循环遍历前的列表
/// 
public static List GetChildCategory(List lists)
{
List categorylist = new List();
for (int i = 0; i )
{
if (0 == lists[i].ParentId)
categorylist.Add(lists[i]);
for (int j = 0; j )
{
if (lists[j].ParentId == lists[i].Id)
lists[i].Children.Add(lists[j]);
}
}
return categorylist;
}

 

然后在Create和Edit的Action中去绑定对应的下拉菜单即可。

注意:List 集合的CategorySelectItemListView,这个是新建的ViewModel对象,用来专门绑定下拉菜单使用,其代码如下:

public class CategorySelectItemListView
{
public int Id { get; set; }
public string Title { get; set; }
public int ParentId { get; set; }
public List Children { get; set; }
public CategorySelectItemListView()
{
Children = new List();
}

public CategorySelectItemListView(int id,string title,int parentid)
{
this.Id = id;
this.Title = title;
this.ParentId = parentid;
Children = new List();
}

public CategorySelectItemListView(int id, string title, CategorySelectItemListView parent)
{
this.Id = id;
this.Title = title;
this.ParentId = parent.Id;
Children = new List();
}

 

(3).视图Edit代码,注解部分的代码可以参考,我尝试用过,也可以达到目的,演练代码最好是用多种方式实现,查看其区别,这样掌握的牢固一些

@{ ViewData["Title"] = "编辑文章"; }
@model Article
@section Scripts{
script type="text/javascript" src="~/js/jquery-3.6.1.min.js">script>
script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "GET",
url: "/ArticleCategory/GetCategory",
data: "{}",
success: function (data) {
console.log(data);
var s = '';
for (var i = 0; i  data.length; i++) {
s += ' + data[i].Title + '"+>' + data[i].Id + '';
}
$("#ArticleCategory").html(s);
}
});
});
script>
}

form action="/Article/Edit" method="post" enctype="multipart/form-data">
@Html.AntiForgeryToken()
div>
label asp-for="Title">标题label>
div>
input type="text" asp-for="Title" name="Title" placeholder="请输入标题">
input type="hidden" asp-for="Id" />
div>
div>
div>
label asp-for="CategoryId">文章类型label>
div>
@Html.DropDownList("ddl_CategoryId", ViewBag.database as IEnumerableSelectListItem>)
div>
div>
div>
label>设置label>
div>
@*@Html.CheckBox("IsTop", Model.IsTop,new { value = Model.IsTop}) 置顶
@Html.CheckBox("IsRed", Model.IsRed, new { value = Model.IsRed }) 热点
@Html.CheckBox("IsSlide", Model.IsSlide, new { value = Model.IsSlide }) 幻灯*@
input asp-for="IsTop" />置顶
input asp-for="IsRed" />热点
input asp-for="IsSlide" />幻灯
@*input type="checkbox" name="IsTop" @(Html.Raw(@Model.IsTop ? "checked="checked"" : "")) asp-for="IsTop" />置顶
input type="checkbox" name="IsRed" @(Html.Raw(@Model.IsRed ? "checked="checked"" : "")) asp-for="IsRed" />热点
input type="checkbox" name="IsSlide" @(Html.Raw(@Model.IsSlide ? "checked="checked"" : "")) asp-for="IsSlide" />幻灯*@
@*input type="checkbox" name="IsTop" asp-for="IsTop" />置顶
input type="checkbox" name="IsRed" asp-for="IsRed"/>热点
input type="checkbox" name="IsSlide" asp-for="IsSlide"/>幻灯*@
div>
div>
div>
label asp-for="ImageUrl">文章首页图label>
div>
input type="file" asp-for="ImageUrl" name="ImageUrl" />
label asp-for="ImageUrl">@Model.ImageUrllabel>
div>
div>
div>
label asp-for="Content">内容label>
div>
textarea placeholder="内容" asp-for="Content" name="Content" cols="30" rows="10">textarea>
div>
div>
div>
label asp-for="Sort">排序label>
div>
input type="text" placeholder="排序" asp-for="Sort" name="Sort" />
div>
div>
div>
label asp-for="ViewCount">点击量label>
div>
input type="text" placeholder="点击量" asp-for="ViewCount" name="ViewCount" />
div>
div>
div>
label asp-for="IsPublish">是否发布label>
div>
select asp-for="IsPublish" name="IsPublish" class="IsPublish">
option value="False">option>
option value="True" selected>option>
select>
div>
div>
div>
label asp-for="Author">作者label>
div>
input type="text" asp-for="Author" name="Author" placeholder="作者名">
div>
div>
div>
label asp-for="Source">来源label>
div>
input type="text" asp-for="Source" name="Source" placeholder="文章来源">
div>
div>
div>
label asp-for="SeoTitle">SEO标题label>
div>
input type="text" asp-for="SeoTitle" name="SeoTitle" placeholder="SEO标题">
div>
div>
div>
label asp-for="SeoKeyword">SEO关键词label>
div>
input type="text" asp-for="SeoKeyword" name="SeoKeyword" placeholder="SEO关键词">
div>
div>
div>
label asp-for="SeoDescription">SEO摘要描述label>
div>
input type="text" asp-for="SeoDescription" name="SeoDescription" placeholder="SEO摘要描述">
div>
div>
div>
div>
button type="submit">确定button>
button type="reset">重置button>
div>
div>
form>

 

(4).视图Index列表的代码

针对列表的显示,又专门编写了ArticeView的这个ViewModel。

篇(18)-Asp.Net Core入门实战-文章管理之文章内容管理(下拉框二级结构递归)插图2

public class ArticleView
{
public int Id { get; set; }
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public string Title { get; set; }
public int ViewCount { get; set; }
public int Sort { get; set; }
public string Author { get; set; }
public string Source { get; set; }
public int AddManagerId { get; set; }
public DateTime AddTime { get; set; }
}

 

@using Humanizer;
@using RjWebCms.Db;
@using RjWebCms.Models.Articles;
@model PaginatedListArticleView>
@{
ViewData["Title"] = "文章列表";
}
@section Scripts{
script src="~/js/jquery-2.1.0.min.js">script>
script type="text/javascript">
function DelAll() {
var ids = document.getElementsByName("#chk_ids");
var arrIds = "";
var n = 0;
for (var i = 0; i  ids.length; i++)
{
if (ids[i].checked == true) {
arrIds += ids[i].value + ",";
n++;
}
}
if (n == 0) {
alert("请选择要删除的信息");
return;
}
arrIds = arrids.substr(0, arrIds.length - 1);
//
if (confirm("确定要全部删除选择信息吗")) {
$.ajax({
type: "post",
url: "/Article/DeleteAll",
data: { ids: arrIds },
success: function (data, state) {
alert('删除成功!');
window.location.href = "";
},

error: function (data, state) {
alert('删除失败');
}
});
}
}
script>
}
div class="panel panel-default todo-panel">
div class="panel-heading">@ViewData["Title"]div>
@Html.AntiForgeryToken()
form asp-action="Index" method="get">
table>
tr>td>a asp-controller="Article" asp-action="Create">添加a>td>tr>
tr>
td>查询关键词:input type="text" name="SearchString" value="@ViewData["CurrentFilter"]" />td>
td>input type="submit" value="查询" />td>
td>a asp-action="Index">Backa>td>
td>a asp-action="DeleteAll">批量删除a>td>
tr>
table>
form>
table class="table table-hover">
thead>
tr>
td>td>
td>a asp-action="Index" asp-route-sortOrder="@ViewData["NameSortParm"]" asp-route-currentFilter="@ViewData["CurrentFilter"]">标题a>td>
td>类别td>
td>a asp-action="Index" asp-route-sortOrder="@ViewData["DateSortParm"]" asp-route-currentFilter="@ViewData["CurrentFilter"]">添加时间a>td>
td>作者td>
td>操作td>
tr>
@foreach (var item in Model)
{
tr>
td>input type="checkbox" class="done-checkbox" name="chk_ids" value="@item.Id">td>
td>@item.Titletd>
td>@item.CategoryNametd>
td>@item.AddTimetd>
td>@item.Authortd>
td>
a asp-action="Details" asp-route-id="@item.Id">Detailsa>
a asp-action="Edit" asp-route-id="@item.Id">Edita>
a asp-action="Delete" asp-route-id="@item.Id">Deletea>
td>
tr>
}
thead>
table>
@{
var prevDisabled = !Model.HasPreviousPage ? "disabled" : "";
var nextDisabled = !Model.HasNextPage ? "disabled" : ""; ;
}

a asp-action="Index"
asp-route-sortOrder="@ViewData["CurrentSort"]"
asp-route-pageNumber="@(Model.PageIndex - 1)"
asp-route-currentFilter="@ViewData["CurrentFilter"]"
class="btn btn-default @prevDisabled">
上一页
a>
a asp-action="Index"
asp-route-sortOrder="@ViewData["CurrentSort"]"
asp-route-pageNumber="@(Model.PageIndex + 1)"
asp-route-currentFilter="@ViewData["CurrentFilter"]"
class="btn btn-default @nextDisabled">
下一页
a>
div class="panel-footer add-item-form">

div>
div>

 

(5).Controller部分的全部代码,注意看代码注释

public class ArticleController : Controller
    {
        private readonly IHostEnvironment _hostEnvironment;
        private readonly IArticleService _articleService;
        private readonly IArticleCategoryService _articleCategoryService;
        private readonly AppDbContext _appDbContext;
        public ArticleController(IArticleService articleService, IArticleCategoryService articleCategoryService,AppDbContext appDbContext,IHostEnvironment hostEnvironment)
        {
            _hostEnvironment = hostEnvironment;
            _appDbContext = appDbContext;
            _articleService = articleService;
            _articleCategoryService = articleCategoryService;
        }

        public async Task Index(string sortOrder, string currentFilter, string searchString, int? pageNumber)
        {
            ViewData["CurrentSort"] = sortOrder;
            ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewData["DateSortParm"] = sortOrder == "Date" ? "date_desc" : "Date";

            if (searchString != null)
            {
                pageNumber = 1;
            }
            else
            {
                searchString = currentFilter;
            }
            ViewData["CurrentFilter"] = searchString;
            var article = from s in _appDbContext.Article
                          join p in _appDbContext.ArticleCategory on s.CategoryId equals p.Id
                          select new ArticleView { 
                            Id = s.Id,
                            CategoryId = s.CategoryId,
                            CategoryName = p.Title,
                            Title = s.Title,
                            Sort = s.Sort,
                            AddManagerId = s.AddManagerId,
                            AddTime = s.AddTime,
                            Author = s.Author,
                            Source = s.Source,
                            ViewCount = s.ViewCount,
                          };
            if (!string.IsNullOrEmpty(searchString))
            {
                article = article.Where(s => s.Title.Contains(searchString));
            }
            switch (sortOrder)
            {
                case "name_desc":
                    article = article.OrderByDescending(s => s.Title) ;
                    break;
                case "Date":
                    article = article.OrderBy(s => s.AddTime);
                    break;
                case "date_desc":
                    article = article.OrderByDescending(s => s.AddTime);
                    break;
                default:
                    article = article.OrderBy(s => s.Title);
                    break;
            }
            int pageSize = 4;
            return View(await PaginatedList.CreateAsync(article.AsNoTracking(), pageNumber ?? 1, pageSize));
        }

        [HttpGet]
        public async Task CreateAsync()
        {
            #region 绑定类别下拉框
            var categories = await _articleCategoryService.GetArticleCategory();//列出文章类别字典
            var categoryItems = new List()
            {
                new SelectListItem(){ Value="0",Text="全部",Selected=true}
            };
            //全部列出并转成DropDownList对象
            List list = new List();
            foreach (var category in categories)
            {
                list.Add(new CategorySelectItemListView { 
                    Id=category.Id,
                    Title = category.Title,
                    ParentId = category.ParentId
                });
            }

            #region 循环嵌套调用
            //List list1 = GetChildCategory(list);
            //foreach (var li in list1)
            //{
            //    categoryItems.Add(new SelectListItem { Value = li.Id.ToString(), Text = li.Title });
            //    if (li.Children.Count > 0)
            //    { 
            //        foreach(var t in li.Children)
            //            categoryItems.Add(new SelectListItem { Value = t.Id.ToString(),Text= "|-" + t.Title });
            //    }
            //}
            #endregion

            #region  递归调用
            List list1 = GetChildCategory(list, new List(), 0);
            foreach (var li in list1)
            {
                categoryItems.Add(new SelectListItem { Value = li.Id.ToString(), Text = li.Title });
                if (li.Children.Count > 0)
                {
                    foreach (var t in li.Children)
                        categoryItems.Add(new SelectListItem { Value = t.Id.ToString(), Text = "  |-" + t.Title });
                }
            }
            #endregion

            ViewBag.database = categoryItems;
            #endregion
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task CreateAsync(Article article,[FromForm]IFormCollection fromData)
        {
            //去掉对字段IsSystem的验证,IsSystem在数据库是bool类型,而前端是0和1,ModelState的验证总是报false,所以去掉对其验证
            //ModelState.Remove("IsSystem");//在View端已经解决了了bool类型,那么此行代码可以不用
            #region 下拉菜单
            string strCategoryId = Request.Form["ddl_CategoryId"];
            if (!string.IsNullOrEmpty(strCategoryId))
                article.CategoryId = int.Parse(strCategoryId);
            else
                article.CategoryId = 0;
            #endregion

            #region 复选框
            article.IsTop = fromData["IsTop"] != "false";//使用FormCollection时,可以这样
            article.IsRed = fromData["IsRed"] != "false";
            article.IsSlide = fromData["IsSlide"] != "false";
            //也可以这样取值,但要注意View内的写法
            //if (!string.IsNullOrEmpty(fromData["IsTop"]))
            //    article.IsTop = true;
            //else
            //    article.IsTop = false;
            #endregion

            #region 上传文件
            IFormFileCollection files = fromData.Files;
           foreach(var formFile in files)
            {
                //var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                string webContentPath = _hostEnvironment.ContentRootPath;
                var fileExt = formFile.FileName.Substring(formFile.FileName.LastIndexOf('.'));//文件扩展名
                var fileNew = DateTime.Now.ToString("yyyyMMddHHmmss") + fileExt; //给文件重新命名

                //string upLoadPath = webContentPath + $@"{fileName}";
                string upLoadPath = webContentPath + $@"UpFiles";
                var fileUrl = upLoadPath + $@"{fileNew}";

                if (formFile.Length > 0)
                {
                    using (var stream = new FileStream(fileUrl,FileMode.Create))
                    {
                        await formFile.CopyToAsync(stream);
                    }
                }
                article.ImageUrl = "../UpFiles/" + fileNew;

            }       
            #endregion 

            if (ModelState.IsValid)
            {
                var successful = await _articleService.AddArticleAysnc(article);
                if (successful)
                    return RedirectToAction("Index");
                else
                    return BadRequest("失败");
            }
            return View(article);
        }

        [HttpGet]
        public async Task Edit(int id)
        {

            if (string.IsNullOrEmpty(id.ToString()))
                return NotFound();

            var article = await _articleService.FindArticleAsync(id);
            if (article == null)
                return NotFound();

            #region 绑定角色下拉框
            var categories = await _articleCategoryService.GetArticleCategory();//列出文章类别字典
            var categoryItems = new List()
            {
                new SelectListItem(){ Value="0",Text="全部",Selected=true}
            };
            //全部列出并转成DropDownList对象
            List list = new List();
            foreach (var category in categories)
            {
                list.Add(new CategorySelectItemListView
                {
                    Id = category.Id,
                    Title = category.Title,
                    ParentId = category.ParentId
                });
            }
            #region  递归调用
            List list1 = GetChildCategory(list, new List(), 0);
            foreach (var li in list1)
            {
                categoryItems.Add(new SelectListItem { Value = li.Id.ToString(), Text = li.Title });
                if (li.Children.Count > 0)
                {
                    foreach (var t in li.Children)
                        categoryItems.Add(new SelectListItem { Value = t.Id.ToString(), Text = "  |-" + t.Title });
                }
            }
            #endregion

            #region 遍历并选中
            foreach (SelectListItem item in categoryItems)
            {
                if (item.Value == article.CategoryId.ToString())
                    item.Selected = true;
            }
            #endregion 
            ViewBag.database = categoryItems;
            #endregion

            return View(article);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task Edit(int id, [FromForm]Article article)
        {
            if (id != article.Id)
            {
                return NotFound();
            }
            #region 下拉菜单
            string strCategoryId = Request.Form["ddl_CategoryId"];
            if (!string.IsNullOrEmpty(strCategoryId))
                article.CategoryId = int.Parse(strCategoryId);
            else
                article.CategoryId = 0;
            #endregion
            #region 复选框
            if (Request.Form["IsTop"].Contains("true"))
                article.IsTop = true;
            else
                article.IsTop = false;
            if (Request.Form["IsRed"].Contains("true"))
                article.IsRed = true;
            else
                article.IsRed = false;
            if (Request.Form["IsSlide"].Contains("true"))
                article.IsSlide = true;
            else
                article.IsSlide = false;
            #endregion 
            //ModelState.Remove("IsTop");
            //ModelState.Remove("IsRed");
            //ModelState.Remove("IsSlide");
            if (ModelState.IsValid)
            {
                try
                {
                    var result = await _articleService.UpdateArticleAsync(id, article);
                    //跳转
                    if (result)
                        return RedirectToAction("Index");
                    else
                        return BadRequest("编辑失败");
                }
                catch (Exception ex)
                {
                    return BadRequest("编辑失败");
                }
            }
            else
            {
                return BadRequest("数据输入有误!");
            }
        }

        /// 
        /// 递归函数,实现获取子菜单
        /// 
        /// 递归前的列表
        /// 递归后的新列表
        /// 父Id
        /// 
        public static List GetChildCategory(List lists, List newlists, int parentId)
        {
            newlists = new List();
            List tempList = lists.Where(c => c.ParentId == parentId).ToList();
            for (int i = 0; i )
            {
                CategorySelectItemListView category = new CategorySelectItemListView();
                category.Id = tempList[i].Id;
                category.ParentId = tempList[i].ParentId;
                category.Title = tempList[i].Title;
                category.Children = GetChildCategory(lists, newlists, category.Id);
                newlists.Add(category);
            }
            return newlists;
        }

        /// 
        /// 循环嵌套,实现获取子菜单
        /// 
        /// 循环遍历前的列表
        /// 
        public static List GetChildCategory(List lists)
        {
            List categorylist = new List();
            for (int i = 0; i )
            {
                if (0 == lists[i].ParentId)
                    categorylist.Add(lists[i]);

                for (int j = 0; j )
                {
                    if (lists[j].ParentId == lists[i].Id)
                        lists[i].Children.Add(lists[j]);
                }
            }
            return categorylist;
        }

    }

 

(6).Service应用层代码

篇(18)-Asp.Net Core入门实战-文章管理之文章内容管理(下拉框二级结构递归)插图3

 

    public class ArticleService : IArticleService
    {
        private readonly AppDbContext _appDbContext;
        public ArticleService(AppDbContext appDbContext)
        {
            _appDbContext = appDbContext;
        }
        /// 
        /// 添加文章
        /// 
        /// 
        /// 
        public async Taskbool> AddArticleAysnc(Article article)
        {
            article.IsDeleted = false;
            article.AddManagerId = 1;//用户id
            article.AddTime = DateTime.Now;
            article.IsPublish = true;
            await _appDbContext.Article.AddAsync(article);
            var result = await _appDbContext.SaveChangesAsync();
            return result == 1;
        }

        /// 
        /// 删除文章
        /// 
        /// 
        /// 
        public async Taskbool> DeleteArticleAsync(int Id)
        {
            var article = await _appDbContext.Article.FirstOrDefaultAsync(x => x.Id == Id);
            if (article != null)
            {
                _appDbContext.Article.Remove(article);
            }
            var result = await _appDbContext.SaveChangesAsync();
            return result == 1; //注意(result==1 如果等式成立,则返回true,说明删除成功)
        }

        /// 
        /// 按Id查询文章
        /// 
        /// 
        /// 
        public async Task
FindArticleAsync(int Id) { var item = await _appDbContext.Article.Where(x => x.Id == Id).FirstOrDefaultAsync(); return item; } /// /// 按标题查询文章 /// /// /// public async Task
GetArtcleByTitle(string title) { var items = await _appDbContext.Article.Where(x => x.Title.Contains(title)).ToArrayAsync(); return items; } /// /// 查询文章 /// /// public async Task
GetArticles() { var items = await _appDbContext.Article.Where(x => x.IsDeleted==false).ToArrayAsync(); return items; } /// /// 更新文章 /// /// /// /// public async Taskbool> UpdateArticleAsync(int id, Article article) { var oldArticle = await FindArticleAsync(id); //找出旧对象 //将新值赋到旧对象上 oldArticle.Title = article.Title; oldArticle.CategoryId = article.CategoryId; oldArticle.SeoDescription = article.SeoDescription; oldArticle.SeoTitle = article.SeoTitle; oldArticle.SeoKeyword = article.SeoKeyword; oldArticle.Content = article.Content; oldArticle.Sort = article.Sort; oldArticle.Source = article.Source; oldArticle.IsSlide = article.IsSlide; oldArticle.IsPublish = article.IsPublish; oldArticle.IsRed = article.IsRed; oldArticle.IsTop = article.IsTop; oldArticle.ViewCount = article.ViewCount; oldArticle.Author = article.Author; oldArticle.ImageUrl = article.ImageUrl; oldArticle.ModifyManagerId = 11;// oldArticle.ModifyTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); //对旧对象执行更新 _appDbContext.Entry(oldArticle).State = EntityState.Modified; var result = await _appDbContext.SaveChangesAsync(); return result == 1; } }

 

 

再谈CheckBox的使用

1.在View视图页增加的代码格式如果为:

置顶

或者是这样:

asp-for="IsTop" />置顶

那么在生成的html代码中,都会自动成id,name,type=“checkbox” value的属性。

2.在Controller中进行取值时的代码为:

if (Request.Form["IsTop"].Contains("true"))

article.IsTop = true;

else

article.IsTop = false;

跟踪时发现,View中Checkbox选中是,会产生true和false两个值,如图跟踪变量发现:

篇(18)-Asp.Net Core入门实战-文章管理之文章内容管理(下拉框二级结构递归)插图4

如此,取值时,就用了Contains功能,因为View中CheckBox没选中,这只有一个false值;

3.在View视图页增加代码的格式如果为:

置顶

4.在Controller中进行取值时的代码为:

if (!string.IsNullOrEmpty(Request.Form["IsTop"]))

article.IsTop = true;

else

article.IsTop = false;

跟踪时发现,View中的CheckBox选中是,取到的值为“on”,如图跟踪发现:

篇(18)-Asp.Net Core入门实战-文章管理之文章内容管理(下拉框二级结构递归)插图5

所以,才用了IsNullOrEmpty这个函数,依据判空来确定是否选中。

但是这样写有个问题,在ModelState.IsValid()的模型验证中,一直无法通过,IsTop一直为false,为此,我干脆就把其去除掉验证:

ModelState.Remove("IsTop");//去除name=IsTop的checkbox的模型验证

5.使用Checkbox,还是要看给在数据表中为其定义的字段类型,Model中的指定类型和验证属性,如果你赋予了Value值,那么就在Controller中取值,Asp.Net Core中Checkbox默认是True和False的值,网上关于@Html.CheckBox()形式也行,你可以尝试跟踪变量值来判断如何处理,其宗旨就是根据具体条件来处理。

文章来源于互联网:篇(18)-Asp.Net Core入门实战-文章管理之文章内容管理(下拉框二级结构递归)

THE END
分享
二维码