今天继续上篇博客的内容,在上一篇的时候,已经基本实现了博客列表内容的显示,继续进行添加、编辑、删除等功能。添加和编辑界面共用一个界面,添加界面如图所示:
同样我这里使用的还是layui里面的表单内容,直接拷贝这个界面:
把没必要的内容删除再稍作修改,直接贴修改后的代码,里面也包含了所有内容:
@model DayaliBlog.Model.Blog.T_BLOG_CONTENT <blockquote class="layui-elem-quote">添加博客</blockquote> <form class="layui-form" method="post" action=""> <div class="layui-form-item"> <label class="layui-form-label">标题</label> <div class="layui-input-block"> @Html.TextBoxFor(a=>a.BlogTitle,new { placeholder = "请输入标题", @class="layui-input"}) </div> </div> <input type="hidden" value="@Model.BlogID" name="BlogID"/> <div class="layui-form-item"> <label class="layui-form-label">博客类型</label> <div class="layui-input-block"> <select name="BlogType" lay-verify="required"> @foreach (DayaliBlog.Model.Sys.T_SYS_CONFIG config in ViewBag.BlogTypes) { if (config.SUB_ID == Model.BlogType) { <option selected="selected" value="@config.SUB_ID">@config.SUB_NM</option> continue; } <option value="@config.SUB_ID">@config.SUB_NM</option> } </select> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">所属分类</label> <div class="layui-input-block"> <select name="CatelogID" lay-verify="required"> @foreach (DayaliBlog.Model.Blog.T_BLOG_CATELOG categ in ViewBag.CategList) { if (categ.CatelogID == Model.CatelogID) { <option selected="selected" value="@categ.CatelogID">@categ.CatelogName</option> continue; } <option value="@categ.CatelogID">@categ.CatelogName</option> } </select> </div> </div> <div class="layui-form-item layui-form-text"> <label class="layui-form-label">内容</label> <div class="layui-input-block"> @Html.TextAreaFor(a=>a.BlogContent,new{ placeholder = "请输入内容" ,@class="layui-textarea"}) </div> </div> <div class="layui-form-item"> <label class="layui-form-label">备注</label> <div class="layui-input-block"> @Html.TextBoxFor(a=>a.Remark,new{ placeholder = "请输入备注", @class = "layui-input"}) </div> </div> <div class="layui-form-item"> <div class="layui-input-block"> <input class="layui-btn" style="max-width: 100px" lay-submit lay-filter="formDemo" type="submit" value="提交"/> </div> </div> </form>
还是使用的是MVC的模式进行绑定数据,不多说了,其中form表单我们采用的是post提交方式,另外最后的提交按钮我已将layui中按钮进行替换了,type="submit",再去完成我们相关的控制器,Index,Add,Del三个Action方法的实现,回到BlogController控制器,还是少说废话,直接贴代码:
using System; using DayaliBlog.Model.Blog; using DayaliBlog.Service.Blog; using DayaliBlog.Service.Sys; using Microsoft.AspNetCore.Mvc; namespace DayaliBlog.Web.Areas.Admin.Controllers { [Area("Admin")] public class BlogController : Controller { BlogCategService _categService=new BlogCategService(); BlogContentService _contentService=new BlogContentService(); BlogCategRelService _relCateg=new BlogCategRelService(); public IActionResult Index() { var list = _contentService.GetList(""); return View(list); } public IActionResult Add(int? id) { ViewBag.BlogTypes = SysConfig.GetConfigList(SysConfig.BlogType); ViewBag.CategList = _categService.GetList(""); T_BLOG_CONTENT content =new T_BLOG_CONTENT(); if (id != null) { content = _contentService.GetModel(" b.BlogID=" + id.Value); } return View(content); } [HttpPost] public IActionResult Add(T_BLOG_CONTENT content) { int blogId = 0; if (content.BlogID == 0) { content.CreateTIme = DateTime.Now; content.CreateUser = 1; content.LastUptTime = DateTime.Now; content.BlogState = 1; blogId= _contentService.Insert(content); } else { blogId = content.BlogID; content.UpdateUser = 1; content.LastUptTime = DateTime.Now; content.BlogState = 1; bool isSuccess=_contentService.Update(content); if (isSuccess) _relCateg.Delete(content.BlogID); } if (blogId > 0) _relCateg.Insert(blogId, content.CatelogID); return Redirect("/Admin/Blog/Index"); } public IActionResult Del(int id) { _relCateg.Delete(id); _contentService.Delete(id); return Redirect("/Admin/Blog/Index"); } } }
几个比较特殊的地方,一个是Add(int? id),因为添加和编辑是同一个界面,当点击添加的时候,传值Id为null,当编辑时传的是BlogID ,以此作为是要插入还是编辑的区分。同样的Add(Model) 方法里面,也是以id为标识,[HttpPost]标识这个Action只能为Post所调用,插入或修改完成后直接返回到首页,return Redirect("/Admin/Blog/Index");
ViewBag.BlogTypes 加载博客类型,ViewBag.CategList 加载所属分类,在前台便利List集合加载下拉框:
@foreach (DayaliBlog.Model.Blog.T_BLOG_CATELOG categ in ViewBag.CategList) { if (categ.CatelogID == Model.CatelogID) { <option selected="selected" value="@categ.CatelogID">@categ.CatelogName</option> continue; } <option value="@categ.CatelogID">@categ.CatelogName</option> }
判断编辑时,当前博客的类型或者分类,将selected属性设置为选中。我的源码地址:DayaliBlogDemo。分类管理在博客中不作说明了,大家可以直接看源码,后台代码写的有点乱糟糟,凑合看吧。
时间: 2024-10-27 13:34:12