04-dotnetCore部落格後台準系統實現

來源:互聯網
上載者:User

標籤:csdn   blog   dex   style   font   返回   介面   pre   模式   

  今天繼續上篇部落格的內容,在上一篇的時候,已經基本實現了部落格列表內容的顯示,繼續進行添加、編輯、刪除等功能。添加和編輯介面共用一個介面,添加介面:

同樣我這裡使用的還是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。分類管理在部落格中不作說明了,大家可以直接看源碼,後台代碼寫的有點亂糟糟,湊合看吧。



04-dotnetCore部落格後台準系統實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.