• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >ASP.NET > ASP.NET MVC5网站开发修改及删除文章(十)

ASP.NET MVC5网站开发修改及删除文章(十)

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-11

通过本文主要向大家介绍了精通asp.net mvc5,精通asp.net mvc5 pdf,pro asp.net mvc5,asp net mvc5,asp mvc5等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

上次做了显示文章列表,再实现修改和删除文章这部分内容就结束了,这次内容比较简单,由于做过了添加文章,修改文章非常类似,就是多了一个TryUpdateModel部分更新模型数据。
一、删除文章
由于公共模型跟,文章,附件有关联,所以这里的删除次序很重要,如果先删除模型,那么文章ModelID和附件的ModelID多会变成null,所以要先先删除文章和附件再删除公共模型。

由于公共模型和附件是一对多的关系,我们把删除公共模型和删除附件写在一起。

在BLL的BaseRepository类中有默认的Delete方法,但这个方法中仅删除模型,不会删除外键,所以在CommonModelRepository在new一个出来封印掉默认的方法。

public new bool Delete(Models.CommonModel commonModel, bool isSave = true)
  {
   if (commonModel.Attachment != null) nContext.Attachments.RemoveRange(commonModel.Attachment);
   nContext.CommonModels.Remove(commonModel);
   return isSave ? nContext.SaveChanges() > 0 : true;
  }
</div>

这个的意思是封印掉继承的Delete方法,在新的方法中如果存在附加那么先删除附件,再删除公共模型。那么如果我们还想调用基类的Delete方法怎么办?可以用base.Delete。

好了,现在可以开始删除了。

在ArticleController中添加Delete方法

/// <summary>
  /// 删除
  /// </summary>
  /// <param name="id">文章id</param>
  /// <returns></returns>
  public JsonResult Delete(int id)
  {
   //删除附件
   var _article = articleService.Find(id);
   if(_article == null) return Json(false);
   //附件列表
   var _attachmentList = _article.CommonModel.Attachment;
   var _commonModel = _article.CommonModel;
   //删除文章
   if (articleService.Delete(_article))
   {
    //删除附件文件
    foreach (var _attachment in _attachmentList)
    {
     System.IO.File.Delete(Server.MapPath(_attachment.FileParth));
    }
    //删除公共模型
    commonModelService.Delete(_commonModel);
    return Json(true);
   }
   else return Json(false);
  }
</div>

二、修改文章
这个部分跟添加文章非常类似
首先在ArticleController中添加显示编辑视图的Edit

/// <summary>
  /// 修改
  /// </summary>
  /// <param name="id"></param>
  /// <returns></returns>
  public ActionResult Edit(int id)
  {
   return View(articleService.Find(id));
  }
</div>

右键添加视图,这个跟添加类似,没什么好说的直接上代码

@section scripts{
 <script type="text/javascript" src="~/Scripts/KindEditor/kindeditor-min.js"></script>
 <script type="text/javascript">
  //编辑框
  KindEditor.ready(function (K) {
   window.editor = K.create('#Content', {
    height: '500px',
    uploadJson: '@Url.Action("Upload", "Attachment")',
    fileManagerJson: '@Url.Action("FileManagerJson", "Attachment", new { id = @Model.CommonModel.ModelID })',
    allowFileManager: true,
    formatUploadUrl: false
   });
  //首页图片
   var editor2 = K.editor({
    fileManagerJson: '@Url.Action("FileManagerJson", "Attachment", new {id=@Model.CommonModel.ModelID })'
   });
   K('#btn_picselect').click(function () {
    editor2.loadPlugin('filemanager', function () {
     editor2.plugin.filemanagerDialog({
      viewType: 'VIEW',
      dirName: 'image',
      clickFn: function (url, title) {
       var url;
       $.ajax({
        type: "post",
        url: "@Url.Action("CreateThumbnail", "Attachment")",
        data: { originalPicture: url },
        async: false,
        success: function (data) {
         if (data == null) alert("生成缩略图失败!");
         else {
          K('#CommonModel_DefaultPicUrl').val(data);
          K('#imgpreview').attr("src", data);
         }
         editor2.hideDialog();
        }
       });
      }
     });
    });
   });
  });
 </script>
}

@model Ninesky.Models.Article
@using (Html.BeginForm())
{ @Html.AntiForgeryToken()
 <div class="form-horizontal" role="form">
  <h4>添加文章</h4>
  <hr />
  @Html.ValidationSummary(true)
  <div class="form-group">
   <label class="control-label col-sm-2" for="CommonModel_CategoryID">栏目</label>
   <div class="col-sm-10">
    <input id="CommonModel_CategoryID" name="CommonModel.CategoryID" data-options="url:'@Url.Action("JsonTree", "Category", new { model="Article" })'" class="easyui-combotree" style="height: 34px; width: 280px;" value="@Model.CommonModel.CategoryID" />
      @Html.ValidationMessageFor(model => model.CommonModel.CategoryID)</div>
  </div>
  <div class="form-group">
   @Html.LabelFor(model => model.CommonModel.Title, new { @class = "control-label col-sm-2" })
   <div class="col-sm-10">
      @Html.TextBoxFor(model => model.CommonModel.Title, new { @class = "form-control" })
      @Html.ValidationMessageFor(model => model.CommonModel.Title)</div>
  </div>


  <div class="form-group">
   @Html.LabelFor(model => model.Author, new { @class = "control-label col-sm-2" })
   <div class="col-sm-10">
    @Html.TextBoxFor(model => model.Author, new { @class = "form-control" })
    @Html.ValidationMessageFor(model => model.Author)
   </div>
  </div>

  <div class="form-group">
   @Html.LabelFor(model => model.Source, new { @class = "control-label col-sm-2" })
   <div class="col-sm-10">
    @Html.TextBoxFor(model => model.Source, new { @class = "form-control" })
    @Html.ValidationMessageFor(model => model.Source)
   </div>
  </div>

  <div class="form-group">
   @Html.LabelFor(model => model.Intro, new { @class = "control-label col-sm-2" })
   <div class="col-sm-10">
    @Html.TextAreaFor(model => model.Intro, new { @class = "form-control" })
    @Html.ValidationMessageFor(model => model.Intro)
   </div>
  </div>

  <div class="form-group">
   @Html.LabelFor(model => model.Content, new { @class = "control-label col-sm-2" })
   <div class="col-sm-10">
    @Html.EditorFor(model => model.Content)
    @Html.ValidationMessageFor(model => model.Content)
   </div>
  </div>

  <div class="form-group">
   @Html.LabelFor(model => model.CommonModel.DefaultPicUrl, new { @class = "control-label col-sm-2" })
   <div class="col-sm-10">
    <img id="imgpreview" class="thumbnail" src="@Model.CommonModel.DefaultPicUrl" />
    @Html.HiddenFor(model => model.CommonModel.DefaultPicUrl)
    <a id="btn_picselect" class="easyui-linkbutton">选择…</a>
    @Html.ValidationMessageFor(model => model.CommonModel.DefaultPicUrl)
   </div>
  </div>

  <div class="form-group">
   <div class="col-sm-offset-2 col-sm-10">
    <input type="submit" value="保存" class="btn btn-default" />
   </div>
  </div>
 </div>
}

</div>

开始做后台接受代码,在ArticleController中添加如下代码。

[HttpPost]
  [ValidateInput(false)]
  [ValidateAntiForgeryToken]
  public ActionResult Edit()
  {
   int _id = int.Parse(ControllerContext.RouteData.GetRequiredString("id"));
   var article = articleService.Find(_id);
   TryUpdateModel(article, new string[] { "Author", "Source", "Intro", "Content" });
   TryUpdateModel(article.CommonModel, "CommonModel", new string[] { "CategoryID", "Title", "DefaultPicUrl" });
   if(ModelState.IsValid)
   {
    if (articleService.Update(article))
    {
     //附件处理
     InterfaceAttachmentService _attachmentService = new AttachmentService();
     var _attachments = _attachmentService.FindList(



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • ASP.NET MVC5 实现分页查询的示例代码
  • ASP.NET MVC5网站开发管理列表、回复及删除(十三)
  • ASP.NET MVC5网站开发我的咨询列表及添加咨询(十二)
  • ASP.NET MVC5网站开发咨询管理的架构(十一)
  • ASP.NET MVC5网站开发修改及删除文章(十)
  • ASP.NET MVC5网站开发显示文章列表(九)
  • ASP.NET MVC5网站开发添加文章(八)
  • ASP.NET MVC5网站开发文章管理架构(七)
  • ASP.NET MVC5网站开发用户修改资料和密码(六)
  • ASP.NET MVC5网站开发用户登录、注销(五)

相关文章

  • 2017-05-11asp.net 身份验证机制实例代码
  • 2017-05-11ASP.NET中的URL过滤实现代码
  • 2017-05-11webapi中如何使用依赖注入
  • 2018-08-20Debian 8或Debian 9(64 位)安装 .NET Core
  • 2017-05-11asp.net 实现防迅雷等下载工具盗链
  • 2017-05-11Visual Studio.Net 内幕(7)
  • 2017-05-11asp.net 验证字符串是否为纯数字检测函数
  • 2017-05-11asp.net开发微信公众平台之验证消息的真实性
  • 2017-05-11asp.net Forms身份验证和基于角色的权限访问
  • 2017-05-11详解ASP.NET提取多层嵌套json数据的方法

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • .Net Core 之 Ubuntu 14.04 部署过程(图文详解)
    • ASP.NET预备知识学习笔记
    • ASP.NET AJAX时用alert弹出对话框
    • 简单几步 实现vs2010对html5的支持
    • ASP.NET中配合JS实现页面计时(定时)自动跳转
    • ASP.NET中常用的用来输出JS脚本的类
    • ASP.NET中的Inherits、CodeFile、CodeBehind的区别详解
    • asp.net下UTF-7转GB2312编码的代码(中文)
    • asp.net Repeater中使用if的代码
    • asp.net读取模版并写入文本文件

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有