Today continue to the content of the blog, in the previous article, has basically implemented the blog list content display, continue to add, edit, delete and other functions. Add and edit interface to share an interface, add interface:
Also I use here is the Layui inside the form content, directly copy this interface:
Delete unnecessary content and then modify it, directly post the modified code, which also contains all the content:
@model dayaliblog.model.blog.t_blog_content<blockquote class= "layui-elem-quote" > Add blog </blockquote> <form class= "Layui-form" method= "POST" action= "" > <div class= "Layui-form-item" > <label class= "Lay Ui-form-label "> Title </label> <div class=" Layui-input-block "> @Html. Textboxfor (A=>a.blogti tle,new {placeholder = "Please enter title", @class = "Layui-input"}) </div> </div> <input type= "hidden" Val Ue= "@Model. BlogID" name= "BlogID"/> <div class= "Layui-form-item" > <label class= "Layui-form-label" > Blog type </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= "LA Yui-form-item "> <label class=" Layui-form-label "> Category </label> <div class=" Layui-input-block "> <select name=" catelogid "lay-verify=" required "> @foreach (DayaliBlog.Model.Blog.T_BL Og_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 clas S= "LayUi-form-item Layui-form-text "> <label class=" Layui-form-label "> Content </label> <div class=" Layu I-input-block "> @Html textareafor (a=>a.blogcontent,new{placeholder =" Please enter content ", @class =" Layui-textarea "}) </div> </div> <div class= "Layui-form-item" > <label class= "Layui-form-label" > Notes </label> <div class= "Layui-input-block" > @Html. Textboxfor (a=>a.remark,new{placeholder = "Please enter Remarks", @class = "Layui-input"}) </div> </div> <div class= "Layui-form-item" > <di V class= "Layui-input-block" > <input class= "layui-btn" style= "max-width:100px" Lay-submit lay-filter= "for Mdemo "type=" Submit "value=" Submission "/> </div> </div></form>
or using the MVC pattern to bind the data, not much to say, where form form we are using the post submission method, and the last commit button I have replaced the Layui button, type= "Submit", then to complete our related controller, Index,add, Del Three action method implementation, back to the BlogController controller, or less nonsense, direct sticker code:
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 BlogControl Ler: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.blogt YPE); 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"); } }}
Some of the more special places, one is the Add (int) ID), because Add and edit is the same interface, when clicked Add, the value of the ID is null, when the editor simultaneous is blogID, as is to insert or edit the distinction. The same add (Model) method is also identified by ID, [HttpPost] identifies the action can only be called for post, insert or modify the completion of the direct return to the home page, return Redirect ("/admin/blog/index");
Viewbag.blogtypes Load Blog type, viewbag.categlist load belongs to category, in the foreground convenient list collection loading dropdown box:
@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> }
To determine the type or classification of the current blog when editing, set the selected property to selected. My source address: Dayaliblogdemo. Classification management in the blog does not make a description, we can directly see the source code, the background codes written a bit messy, make a look at it.
04-dotnetcore Blog Background basic ability to implement