五、文章

在【ArticleController】添加[UserAdd()]action

/// <summary>/// 添加文章/// </summary>
        [UserAuthorize]public ActionResult UserAdd(){return View(new Article() { CommonModel = new CommonModel() });}

右键添加模型类为Article的强类型视图UserAdd.cshtml。编辑器选用KindEditor(国货当自强)……

@model Ninesky.Models.Article@{ViewBag.Title = "添加文章";Layout = "~/Views/Shared/_User.cshtml";
}<div class="workspace"><div class="inside"><div class="notebar"><img alt="" src="~/Content/Default/User/Images/Icon/Article_16.png" />您现在的位置: 文章管理</div><div>@using (Html.BeginForm()){@Html.ValidationSummary(true)<div class="formset"><dl><dt>添加文章</dt><dd><div class="editor-label">@Html.LabelFor(model => model.CommonModel.CategoryId) (必填) @Html.DisplayDescriptionFor(model => model.CommonModel.CategoryId)</div><div class="editor-field">@Html.HiddenFor(model => model.CommonModel.CategoryId)<input type="text" class = "combotree" id="CommonModel_CategoryId_Text" />@Html.ValidationMessageFor(model => model.CommonModel.CategoryId)<br /><ul id="categorytree" class="ztree comboztree"><li>222</li></ul></div></dd><dd><div class="editor-label">@Html.LabelFor(model => model.CommonModel.Title) (必填) @Html.DisplayDescriptionFor(model => model.CommonModel.Title)</div><div class="editor-field">@Html.EditorFor(model => model.CommonModel.Title)@Html.ValidationMessageFor(model => model.CommonModel.Title)</div></dd><dd><div class="editor-label">@Html.LabelFor(model => model.Source)@Html.DisplayDescriptionFor(model => model.Source)</div><div class="editor-field">@Html.EditorFor(model => model.Source)@Html.ValidationMessageFor(model => model.Source)</div></dd><dd><div class="editor-label">@Html.LabelFor(model => model.Intro)@Html.DisplayDescriptionFor(model => model.Intro)</div><div class="editor-field">@Html.TextAreaFor(model => model.Intro, new { @class = "Intro" })@Html.ValidationMessageFor(model => model.Intro)</div></dd><dd><div class="editor-label">@Html.LabelFor(model => model.CommonModel.PicUrl)@Html.DisplayDescriptionFor(model => model.CommonModel.PicUrl)</div><div class="editor-field">@Html.EditorFor(model => model.CommonModel.PicUrl)@Html.ValidationMessageFor(model => model.CommonModel.PicUrl)</div></dd><dd><div class="editor-label">@Html.LabelFor(model => model.CommonModel.Status)@Html.DisplayDescriptionFor(model => model.CommonModel.Status)</div><div class="editor-field">@Html.DropDownListFor(model => model.CommonModel.Status,Ninesky.Models.CommonModel.ContentStatus)@Html.ValidationMessageFor(model => model.CommonModel.Status)</div></dd><dd><div class="editor-label">@Html.LabelFor(model => model.CommonModel.ReleaseDate) (必填) @Html.DisplayDescriptionFor(model => model.CommonModel.ReleaseDate)</div><div class="editor-field">@Html.TextBoxFor(model => model.CommonModel.ReleaseDate, new { @class = "datepicker", type = "datetime" })@Html.ValidationMessageFor(model => model.CommonModel.ReleaseDate)</div></dd><dd><div class="editor-label">@Html.LabelFor(model => model.Content) (必填) @Html.DisplayDescriptionFor(model => model.Content)</div><div class="editor-field">@Html.TextAreaFor(model => model.Content, new { @class = "content" })@Html.ValidationMessageFor(model => model.Content)</div></dd><dd><div class="editor-label"></div><div class="editor-field"><input type="submit" value="发布" /></div></dd><dd><div class="editor-label"></div><div class="editor-field"></div></dd></dl></div>}</div></div>
</div>
<div class="left">@Html.Partial("PartialUserNavMenus")<br /></div>
<div class="clear"></div>
<script type="text/javascript">KindEditor.ready(function (K) {K.create('#Content');});var setting = {view: { selectedMulti: false },callback: {onClick: zTreeOnClick}};$(document).ready(function () {$.post("@Url.Action("JsonUserGeneralTree", "Category")", { model: "Article" }, function (data) {var zTree = $.fn.zTree.init($("#categorytree"), setting, data);zTree.expandAll(true);});$("#CommonModel_CategoryId_Text").click(function () {$("#categorytree").show();});});function zTreeOnClick(event, treeId, treeNode) {if (treeNode.iconSkin == "canadd") {$("#CommonModel_CategoryId").val(treeNode.id);$("#CommonModel_CategoryId_Text").val(treeNode.name);$("#categorytree").hide();}else {alert("该栏目不能文章");}};$("#CommonModel_ReleaseDate").datepicker({})$("input[type='submit']").button();</script>
@section Scripts {@Styles.Render("~/ZtreeCss")@Scripts.Render("~/bundles/jqueryval")@Scripts.Render("~/Ztree")@Scripts.Render("~/bundles/kindeditor")
}

F5一下

再次打开【ArticleController】添加[UserAdd(Article article)action。在action上添加[ValidateInput(false)]属性停用对用户输入内容的验证。

[HttpPost][UserAuthorize][ValidateInput(false)]public ActionResult UserAdd(Article article){//验证栏目CategoryRepository _categoryRsy = new CategoryRepository();var _category = _categoryRsy.Find(article.CommonModel.CategoryId);if (_category == null) ModelState.AddModelError("CommonModel.CategoryId", "栏目不存在");if(_category.Model != "Article") ModelState.AddModelError("CommonModel.CategoryId", "该栏目不能添加文章!");article.CommonModel.Inputer = UserController.UserName;ModelState.Remove("CommonModel.Inputer");article.CommonModel.Model = "Article";ModelState.Remove("CommonModel.Model");if (ModelState.IsValid){if (articleRsy.Add(article)){Notice _n = new Notice { Title = "添加文章成功", Details = "您已经成功添加[" + article.CommonModel.Title + "]文章!", DwellTime = 5, NavigationName = "我的文章", NavigationUrl = Url.Action("UserOwn", "Article") };return RedirectToAction("UserNotice", "Prompt", _n);}else{Error _e = new Error { Title = "添加文章失败", Details = "在添加文章时,未能保存到数据库", Cause = "系统错误", Solution = Server.UrlEncode("<li>返回<a href='" + Url.Action("UserAdd", "Article") + "'>添加文章</a>页面,输入正确的信息后重新操作</li><li>返回<a href='" + Url.Action("UserDefault", "Article") + "'>文章管理首页</a>。</li><li>联系网站管理员</li>") };return RedirectToAction("ManageError", "Prompt", _e);}}return View(article);}

action中首先检查提交过来的栏目ID对应的栏目是否存在,再次验证该栏目是否运行添加文章,然后设置发布者和模型,最后保存到数据库。

F5 浏览器中测试一下。

添加成功

=============================================

代码见 五、文章

转载于:https://www.cnblogs.com/mzwhj/archive/2013/01/20/2868948.html

学用MVC4做网站五:5.1添加文章相关推荐

  1. 学用MVC4做网站五:5.4删除文章

    前几天把添加.修改功能都做了,今天开始写删除功能.删除文章既要删除文章本身同时也要在公共模型中删除对应项. 首先写从数据库中删除文章的函数.打开ArticleRepository修改Delete的函数 ...

  2. 学用MVC4做网站二:2.2添加用户组

    一.用户 二.用户组 2.1浏览用户组 2.2添加用户组 修改[Add]Action /// <summary>/// 添加用户组/// </summary>/// <r ...

  3. 学用MVC4做网站四:公共模型

    网站内可能包含文章.留言.产品等,这些内容都有像标题.发布日期,发布人,所属栏目--一部分共同数据,把这些数据做个公共模型放到一个单独模型中. 字段 名称 类型 必填 默认值 说明 CommonMod ...

  4. 计算机考试字处理怎样加水印,一学即会!图片上如何添加文字水印

    原标题:一学即会!图片上如何添加文字水印 平时工作中,有的小伙伴经常需要处理一大堆的图片,比如说在网上发表自己的文章,在购物网站上晒单,还是自己亲手拍的照片,自己制作的图片,或者画出来一个好的创意,想 ...

  5. 8天学通MongoDB——第五天 主从复制

    随笔 - 218  文章 - 1  评论 - 3819 8天学通MongoDB--第五天 主从复制 从这一篇开始我们主要讨论mongodb的部署技术. 我们知道sql server能够做到读写分离,双 ...

  6. uwp post php,window_Win10开发系列专题五 UWP应用添加画布及语音输入支持,这是微软Win10十个开发系列专 - phpStudy...

    Win10开发系列专题五 UWP应用添加画布及语音输入支持 这是微软Win10十个开发系列专题的第五期内容,本期微软讲解了为Windows10 UWP应用添加画布/数字墨水书写及语音输入支持的方法.微 ...

  7. python与php做网站_学Python对做网站用处大吗?

    Python是一种简单的编程语言,用于网站开发用处当然是非常大的,无可厚非, 相对于Java.C.PHP.C++ 和 VB. Python作为一个很年轻的语言,是相当的优秀.著名的搜索引擎 Googl ...

  8. PyQT5一起学做图书管理系统(6)添加/删除书籍

    PyQT5一起学做图书管理系统(6)添加/删除书籍 环境 系统:windows10系统 编辑器:Sublime3 编程语言:python3+pyqt5 前言 本节来实现书籍的添加与删除,还是那句话,记 ...

  9. 《动手学ROS2》9.5为FishBot添加添加激光雷达传感器

    <动手学ROS2>9.5为FishBot添加添加激光雷达传感器 本系列教程作者:小鱼 公众号:鱼香ROS QQ交流群:139707339 教学视频地址:小鱼的B站 完整文档地址:鱼香ROS ...

最新文章

  1. 用Python解锁“吃鸡”正确姿势
  2. 推荐一款基于 SpringBoot 的接口快速开发框架
  3. PHP 4.4.7 中用 PEAR 类库操作 ZIP 压缩文件
  4. Rust即将发布1.0版本,Go持续获得关注:如何在新生语言之间做出抉择
  5. Zookeeper之Leader选举源码分析
  6. JAVA加密算法(DSA)
  7. oracle日常函数应用,oracle日常 常用函数与脚本
  8. STL源码剖析 Set相关算法 并集 set_union|交集 set_intersection|差集 set_difference |对称差集 set_symmetric_difference
  9. php 日期时间运算的小结(计算当前时间之后(之前)的时间)
  10. 聊聊spring-boot-starter-data-redis的配置变更
  11. VCL组件之Name属性
  12. HDU2188 选拔志愿者【巴什博弈】
  13. 计算机考研数据结构参考书,2016计算机考研:数据结构参考书及其复习重点
  14. 软件测试中的43个功能测试点(上)
  15. C#实现简单音乐播放器
  16. C语言——是否为闰年的判断
  17. gitlab CI/CD 持续集成 部署 一文到底
  18. html中快速复制上一行并粘贴到下一行
  19. 从四大云计算公司走向 看云行业趋势
  20. matlab画出鸢尾花数据集散点图尾花,鸢尾花数据集分以及绘制散点矩阵图

热门文章

  1. 递归_三要素_基础算法必备
  2. 常见Linux面试题总结
  3. python学习之自习语法(20180626_update)
  4. jquery中ajax格式
  5. JavaScript、Jquery:获取各种屏幕的宽度和高度
  6. Gradient Boost Decision Tree(GBDT)中损失函数为什么是对数形式
  7. 积跬步,聚小流-------js实现placeholder的效果
  8. Daily Report 2012.11.9 刘宇翔
  9. InnoDB与MyISAM的六大区别(转)
  10. PHP学习:文件操作