public async Task <ViewBlog> GetBlogAsync(string name) { try { ViewBlog ap = null; if (!CommonTools.isEmpty(name)) { ap = new ViewBlog(); var tap = (await this.ListBlog()).First(x => x.Name == name); ap.ImportFromModel(tap); if (ap.Categories == null) { ap.Categories = new List <Category>(); } if (ap.Posts == null) { ap.Posts = new List <Post>(); } } return(ap); } catch (Exception ex) { CommonTools.ErrorReporting(ex); return(null); } }
public ActionResult Blog(int?id) { var currentLoginUser = Session["loginuser"] == null ? null : (BlogUser)Session["loginuser"]; ViewBag.currentLoginInfo = currentLoginUser; int tid; if (id == null) { return(Redirect("/home")); } else { tid = (int)id; } try { var model = home.GetBlog(tid); var cmt = home.GetBlogComment(tid); var viewmodel = new ViewBlog { TextID = model.TextID, TextTitle = model.TextTitle, TextChangeDate = model.TextChangeDate, Hot = model.Hot, Text = model.Text, PreID = model.PreID, NexID = model.NexID, FirstView = model.FirstView, CategoryName = model.CategoryName }; //此处参数length用于修改博文显示时前后文标题的长度限制 int length = 18; if (viewmodel.PreID != 0) { viewmodel.PreTitle = home.GetBlogFree(model.PreID).TextTitle; if (viewmodel.PreTitle.Length > length) { viewmodel.PreTitle = viewmodel.PreTitle.Substring(0, length) + "..."; } } if (viewmodel.NexID != 0) { viewmodel.NexTitle = home.GetBlogFree(model.NexID).TextTitle; if (viewmodel.NexTitle.Length > length) { viewmodel.NexTitle = viewmodel.NexTitle.Substring(0, length) + "..."; } } ViewBag.CmtList = cmt; return(View(viewmodel)); } catch (Exception) { return(Redirect("/home")); } }
// GET: Blogs public async Task <IActionResult> Index() { try { List <ViewBlog> lstblogs = new List <ViewBlog>(); var list = await blogmnger.ListBlog(); foreach (var bl in list) { ViewBlog vb = new ViewBlog(); vb.ImportFromModel(bl); lstblogs.Add(vb); } return(View(lstblogs)); } catch (Exception) { throw; } }