日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

学用MVC4做网站五:5.1添加文章

發布時間:2024/6/14 c/c++ 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 学用MVC4做网站五:5.1添加文章 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

五、文章

在【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添加文章的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。