ASP.NET MVC5网站开发管理列表、回复及删除(十三)

所属分类: 网络编程 / ASP.NET 阅读数: 1897
收藏 0 赞 0 分享

一、管理列表

跟上次我的列表相似,直接贴代码了。

首先打开Consultation控制器,添加ManageList方法

/// <summary>
  /// 咨询管理
  /// </summary>
  /// <returns></returns>
  public ActionResult ManageList()
  {
   return View();
  }

添加返回json数据的ManageJsonList

public JsonResult ManageJsonList(int pageIndex = 1, int pageSize = 20)
  {
   int _total;
   var _list = commonModelService.FindPageList(out _total, pageIndex, pageSize, "Consultation", string.Empty, 0, string.Empty, null, null, 0).ToList().Select(
    cm => new Ninesky.Web.Models.CommonModelViewModel()
    {
     CategoryID = cm.CategoryID,
     CategoryName = cm.Category.Name,
     DefaultPicUrl = cm.DefaultPicUrl,
     Hits = cm.Hits,
     Inputer = cm.Inputer,
     Model = cm.Model,
     ModelID = cm.ModelID,
     ReleaseDate = cm.ReleaseDate,
     Status = cm.Status,
     Title = cm.Title
    });
   return Json(new { total = _total, rows = _list.ToList() });
  }

右键为ManageList添加试图

@{
 ViewBag.Title = "咨询管理";
}

<div id="toolbar">
 <div>
  <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="del()">删除</a>
  <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true" onclick="$('#Consultation_List').datagrid('reload');">刷新</a>
 </div>
</div>

<table id="Consultation_List"></table>

<script src="~/Scripts/Common.js"></script>
<script src="~/Scripts/jquery.easyui.datagrid.detailview.js"></script>
<script type="text/javascript">
 $("#Consultation_List").datagrid({
  loadMsg: '加载中……',
  fitColumns: true,
  pagination: true,
  url: '@Url.Action("ManageJsonList", "Consultation")',
  columns: [[
   { field: 'ModelID', title: 'ID', checkbox: true },
   { field: 'Title', title: '标题' },
   { field: 'Inputer', title: '咨询人', align: 'right' },
   { field: 'ReleaseDate', title: '咨询日期', align: 'right', formatter: function (value, row, index) { return jsonDateFormat(value); } },
   { field: 'StatusString', title: '状态', width: 100, align: 'right' }
  ]],
  toolbar: '#toolbar',
  idField: 'ModelID',
  view: detailview,
  detailFormatter: function (rowIndex, rowData) { return '<div class="detail" style="width:100%,padding:5px 0"></div>'; },
  onExpandRow: function (index, row) {
   var detail = $(this).datagrid('getRowDetail', index).find('div.detail');
   $(detail).html("<iframe frameborder='0' marginwidth='0' height='160px' width='100%' src='@Url.Action("Reply", "Consultation")/" + row.ModelID + "'></iframe>");
   $('#Consultation_List').datagrid('fixDetailRowHeight', index);
  }
 });

</script>

二、回复评论

ManageList添加datagrid详细视图使用类框架(("<iframe frameborder='0' marginwidth='0' height='160px' width='100%' src='@Url.Action("Reply", "Consultation")/" + row.ModelID + "'></iframe>")。“Consultation/Reply”就是我们回复的视图。

在Consultation控制器,添加Reply方法

/// <summary>
  /// 回复
  /// </summary>
  /// <param name="id">id</param>
  /// <returns></returns>
  public ActionResult Reply(int id)
  {
   return View(commonModelService.Find(id).Consultation);
  }

右键添加视图

@model Ninesky.Models.Consultation
@using (Html.BeginForm())
{
 @Html.AntiForgeryToken()
 <table style="width:100%;font-size:12px;">
  <tr>
   <th>@Html.DisplayNameFor(model => model.Name)</th>
   <td>@Html.DisplayFor(model => model.Name)</td>
   <th>@Html.DisplayNameFor(model => model.IsPublic)</th>
   <td>@Html.DisplayFor(model => model.IsPublic)</td>
  </tr>
  <tr>
   <th>@Html.DisplayNameFor(model => model.QQ)</th>
   <td>@Html.DisplayFor(model => model.QQ)</td>
   <th>@Html.DisplayNameFor(model => model.Email)</th>
   <td>@Html.DisplayFor(model => model.Email)</td>
  </tr>
  <tr>
   <th>@Html.DisplayNameFor(model => model.Content)</th>
   <td colspan="3">@Html.DisplayFor(model => model.Content)</td>
  </tr>
  @if (Model.ReplyTime != null)
  {
   <tr>
    <td colspan="4">

     <span>管理员于:@Model.ReplyTime 回复如下</span>
     <br />
     <p style=" margin-top:8px">
      @Model.ReplyContent
     </p>

    </td>
   </tr>
  }
  else
  {
   <tr>
    <th>
     回复 @Html.HiddenFor(model => model.ConsultationID) 
     @Html.ValidationMessageFor(model=>model.ConsultationID)
    </th>
    <td colspan="3">
     @Html.TextAreaFor(model => model.ReplyContent, new { @class = "form-control" })
     @Html.ValidationMessageFor(model=>model.ReplyContent)
    </td>
   </tr>
   <tr>
    <th>

    </th>
    <td colspan="3">
     <input type="submit" class="btn_reply btn btn-primary" value="确定" />
    </td>
   </tr>
  }
 </table>
}

添加接收处理的方法。

[HttpPost]
  [ValidateAntiForgeryToken]
  public ActionResult Reply()
  {
   CommonModel _commonModel = null;
   if (RouteData.Values.ContainsKey("id"))
   {
    int _modelId = int.Parse(RouteData.Values["id"].ToString());
    _commonModel = commonModelService.Find(_modelId);
    if (string.IsNullOrEmpty(Request.Form["ReplyContent"])) ModelState.AddModelError("ReplyContent", "必须输入回复内容!");
    else
    {
     _commonModel.Consultation.ReplyContent = Request.Form["ReplyContent"];
     _commonModel.Consultation.ReplyTime = System.DateTime.Now;
     _commonModel.Status = 29;
     commonModelService.Update(_commonModel);
    }
   }
   return View(_commonModel.Consultation);
  }

过程是:

1、接收路由中的id参数(RouteData.Values.ContainsKey("id"))

2、查找该ID的CommonModel,并获取客户端传过来的ReplyContent,设置其他参数(ReplyTime,Status)并保存到数据库

3、返回视图

三、删除评论

在Consultation控制器,添加Delete方法

/// <summary>
  /// 删除评论
  /// </summary>
  /// <param name="id">公共模型ID</param>
  /// <returns></returns>
  public ActionResult Delete(int id)
  {
   var _commonModel = commonModelService.Find(id);
   if (_commonModel == null) return Json(false);

   if (commonModelService.Delete(_commonModel)) return Json(true);
   else return Json(false);
  }
然后打开ManageList视图,添加删除js代码
//删除
 function del() {
  var rows = $("#Consultation_List").datagrid("getSelections");
  if (!rows || rows.length < 1) {
   $.messager.alert("提示", "未选择任何行!");
   return;
  }
  else if (rows.length > 0) {
   $.messager.confirm("确认", "您确定要删除所选行吗?", function (r) {
    if (r) {
     $.messager.progress();
     $.each(rows, function (index, value) {
      $.ajax({
       type: "post",
       url: "@Url.Action("Delete", "Consultation")",
       data: { id: value.ModelID },
       async: false,
       success: function (data) {
       }
      });
     });
     $.messager.progress('close');
     //清除选择行
     rows.length = 0;
     $("#Consultation_List").datagrid('reload');
    }
   });
   return;
  }

本文已被整理到了《ASP.NET MVC网站开发教程》,欢迎大家学习阅读,更多内容还可以参考ASP.NET MVC5网站开发专题学习。

这次的内容比较重复,管理列表类似与我的咨询列表,删除、回复与文章的代码很类似,关于member区域终于写完,希望对大家有所帮助。

更多精彩内容其他人还在看

Asp.Net二级域名共享Forms身份验证、下载站/图片站的授权访问控制

我们平时一般在做图片或者文件下载权限控制的时候基本都是控制到下载页面的,当你的下载地址暴露后,浏览者就直接可以通过文件地址进行下载了,这时候也就出现了我们常说的盗链
收藏 0 赞 0 分享

在ASP.NET中下载文件的实现代码

通过ASP.NET来下载文件,这个问题可大可小,我们先从小的开始。当我们要让用户下载一个文件
收藏 0 赞 0 分享

asp.net下日期和时间处理的类库

发一个专门处理时间和日期的类库,记录以备查询
收藏 0 赞 0 分享

LINQ重写博客垃圾图片回收算法

本人博客后台管理模块有个功能,可以扫描图片上传文件夹下所有未被引用的博客
收藏 0 赞 0 分享

C#多线程Singleton(单件)模式模板

下面是一个C#多线程单件模式的代码模板。把T换成你自己的类型就可以使用了。其精妙之处就在于用lock语句锁定资源来避免多线程同时走入if语句去创建多个对象
收藏 0 赞 0 分享

URL重写及干掉ASP.NET试图状态的实现方法

URL重写已经很普遍了,但基本上大部分的URL重写都不支持页面的相对路径,所有如果想在已经开发好的项目中添加还是有压力的,第二就是例如微软的那个URL重写是根据正则表达式来处理的,那样是很好,但也有不足之处,就是不方便定位到某个页面只能有哪些参数
收藏 0 赞 0 分享

正则方式的自动小偷抓网程序

公司里面有许多数据没人去录入,做一个抓取网页的程序,以前做CMS系统的时候涉及过,不过这次的处理HTML上和以前做了些区别
收藏 0 赞 0 分享

asp.net生成缩略图实现代码

此文件imgSmall.ashx专门用来生成图片的缩略图,可以减少服务器压力,降低网络流量,初学者必备
收藏 0 赞 0 分享

asp.net richTextBox中高亮显示选中字符串或文本

最近开发程序需要对一段文本中的某个字符串进行高亮显示,网上找了下资料
收藏 0 赞 0 分享

ASP.net的验证控件浅析

前些天在做注册页面的验证的时候,用了下ASP.net的验证控件,有一些体会,特写下这篇博客,如果有朋友有不同ideas,欢迎大家留言
收藏 0 赞 0 分享
查看更多