学用MVC4做网站五:5.1添加文章
生活随笔
收集整理的這篇文章主要介紹了
学用MVC4做网站五:5.1添加文章
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
五、文章
在【ArticleController】添加[UserAdd()]action
/// <summary>/// 添加文章/// </summary> [UserAuthorize]public ActionResult UserAdd(){return View(new Article() { CommonModel = new CommonModel() });}右鍵添加模型類為Article的強(qiáng)類型視圖UserAdd.cshtml。編輯器選用KindEditor(國(guó)貨當(dāng)自強(qiáng))……
@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" />您現(xiàn)在的位置: 文章管理</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="發(fā)布" /></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一下
再次打開(kāi)【ArticleController】添加[UserAdd(Article article)action。在action上添加[ValidateInput(false)]屬性停用對(duì)用戶輸入內(nèi)容的驗(yàn)證。
[HttpPost][UserAuthorize][ValidateInput(false)]public ActionResult UserAdd(Article article){//驗(yàn)證欄目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 = "您已經(jīng)成功添加[" + article.CommonModel.Title + "]文章!", DwellTime = 5, NavigationName = "我的文章", NavigationUrl = Url.Action("UserOwn", "Article") };return RedirectToAction("UserNotice", "Prompt", _n);}else{Error _e = new Error { Title = "添加文章失敗", Details = "在添加文章時(shí),未能保存到數(shù)據(jù)庫(kù)", Cause = "系統(tǒng)錯(cuò)誤", Solution = Server.UrlEncode("<li>返回<a href='" + Url.Action("UserAdd", "Article") + "'>添加文章</a>頁(yè)面,輸入正確的信息后重新操作</li><li>返回<a href='" + Url.Action("UserDefault", "Article") + "'>文章管理首頁(yè)</a>。</li><li>聯(lián)系網(wǎng)站管理員</li>") };return RedirectToAction("ManageError", "Prompt", _e);}}return View(article);}action中首先檢查提交過(guò)來(lái)的欄目ID對(duì)應(yīng)的欄目是否存在,再次驗(yàn)證該欄目是否運(yùn)行添加文章,然后設(shè)置發(fā)布者和模型,最后保存到數(shù)據(jù)庫(kù)。
F5 瀏覽器中測(cè)試一下。
添加成功
=============================================
代碼見(jiàn) 五、文章
轉(zhuǎn)載于:https://www.cnblogs.com/mzwhj/archive/2013/01/20/2868948.html
總結(jié)
以上是生活随笔為你收集整理的学用MVC4做网站五:5.1添加文章的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 一块网卡设置多个IP地址
- 下一篇: Linux编程基础——GDB(查看数据)