当前位置:Gxlcms > asp.net > ASP.NET MVC5网站开发管理列表、回复及删除(十三)

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

时间:2021-07-01 10:21:17 帮助过:16人阅读

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

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

  1. /// <summary>
  2. /// 咨询管理
  3. /// </summary>
  4. /// <returns></returns>
  5. public ActionResult ManageList()
  6. {
  7. return View();
  8. }

添加返回json数据的ManageJsonList

  1. public JsonResult ManageJsonList(int pageIndex = 1, int pageSize = 20)
  2. {
  3. int _total;
  4. var _list = commonModelService.FindPageList(out _total, pageIndex, pageSize, "Consultation", string.Empty, 0, string.Empty, null, null, 0).ToList().Select(
  5. cm => new Ninesky.Web.Models.CommonModelViewModel()
  6. {
  7. CategoryID = cm.CategoryID,
  8. CategoryName = cm.Category.Name,
  9. DefaultPicUrl = cm.DefaultPicUrl,
  10. Hits = cm.Hits,
  11. Inputer = cm.Inputer,
  12. Model = cm.Model,
  13. ModelID = cm.ModelID,
  14. ReleaseDate = cm.ReleaseDate,
  15. Status = cm.Status,
  16. Title = cm.Title
  17. });
  18. return Json(new { total = _total, rows = _list.ToList() });
  19. }

右键为ManageList添加试图

  1. @{
  2. ViewBag.Title = "咨询管理";
  3. }
  4. <div id="toolbar">
  5. <div>
  6. <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="del()">删除</a>
  7. <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true" onclick="$('#Consultation_List').datagrid('reload');">刷新</a>
  8. </div>
  9. </div>
  10. <table id="Consultation_List"></table>
  11. <script src="~/Scripts/Common.js"></script>
  12. <script src="~/Scripts/jquery.easyui.datagrid.detailview.js"></script>
  13. <script type="text/javascript">
  14. $("#Consultation_List").datagrid({
  15. loadMsg: '加载中……',
  16. fitColumns: true,
  17. pagination: true,
  18. url: '@Url.Action("ManageJsonList", "Consultation")',
  19. columns: [[
  20. { field: 'ModelID', title: 'ID', checkbox: true },
  21. { field: 'Title', title: '标题' },
  22. { field: 'Inputer', title: '咨询人', align: 'right' },
  23. { field: 'ReleaseDate', title: '咨询日期', align: 'right', formatter: function (value, row, index) { return jsonDateFormat(value); } },
  24. { field: 'StatusString', title: '状态', width: 100, align: 'right' }
  25. ]],
  26. toolbar: '#toolbar',
  27. idField: 'ModelID',
  28. view: detailview,
  29. detailFormatter: function (rowIndex, rowData) { return '<div class="detail" style="width:100%,padding:5px 0"></div>'; },
  30. onExpandRow: function (index, row) {
  31. var detail = $(this).datagrid('getRowDetail', index).find('div.detail');
  32. $(detail).html("<iframe frameborder='0' marginwidth='0' height='160px' width='100%' src='@Url.Action("Reply", "Consultation")/" + row.ModelID + "'></iframe>");
  33. $('#Consultation_List').datagrid('fixDetailRowHeight', index);
  34. }
  35. });
  36. </script>


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

在Consultation控制器,添加Reply方法

  1. /// <summary>
  2. /// 回复
  3. /// </summary>
  4. /// <param name="id">id</param>
  5. /// <returns></returns>
  6. public ActionResult Reply(int id)
  7. {
  8. return View(commonModelService.Find(id).Consultation);
  9. }

右键添加视图

  1. @model Ninesky.Models.Consultation
  2. @using (Html.BeginForm())
  3. {
  4. @Html.AntiForgeryToken()
  5. <table style="width:100%;font-size:12px;">
  6. <tr>
  7. <th>@Html.DisplayNameFor(model => model.Name)</th>
  8. <td>@Html.DisplayFor(model => model.Name)</td>
  9. <th>@Html.DisplayNameFor(model => model.IsPublic)</th>
  10. <td>@Html.DisplayFor(model => model.IsPublic)</td>
  11. </tr>
  12. <tr>
  13. <th>@Html.DisplayNameFor(model => model.QQ)</th>
  14. <td>@Html.DisplayFor(model => model.QQ)</td>
  15. <th>@Html.DisplayNameFor(model => model.Email)</th>
  16. <td>@Html.DisplayFor(model => model.Email)</td>
  17. </tr>
  18. <tr>
  19. <th>@Html.DisplayNameFor(model => model.Content)</th>
  20. <td colspan="3">@Html.DisplayFor(model => model.Content)</td>
  21. </tr>
  22. @if (Model.ReplyTime != null)
  23. {
  24. <tr>
  25. <td colspan="4">
  26. <span>管理员于:@Model.ReplyTime 回复如下</span>
  27. <br />
  28. <p style=" margin-top:8px">
  29. @Model.ReplyContent
  30. </p>
  31. </td>
  32. </tr>
  33. }
  34. else
  35. {
  36. <tr>
  37. <th>
  38. 回复 @Html.HiddenFor(model => model.ConsultationID)
  39. @Html.ValidationMessageFor(model=>model.ConsultationID)
  40. </th>
  41. <td colspan="3">
  42. @Html.TextAreaFor(model => model.ReplyContent, new { @class = "form-control" })
  43. @Html.ValidationMessageFor(model=>model.ReplyContent)
  44. </td>
  45. </tr>
  46. <tr>
  47. <th>
  48. </th>
  49. <td colspan="3">
  50. <input type="submit" class="btn_reply btn btn-primary" value="确定" />
  51. </td>
  52. </tr>
  53. }
  54. </table>
  55. }

添加接收处理的方法。

  1. [HttpPost]
  2. [ValidateAntiForgeryToken]
  3. public ActionResult Reply()
  4. {
  5. CommonModel _commonModel = null;
  6. if (RouteData.Values.ContainsKey("id"))
  7. {
  8. int _modelId = int.Parse(RouteData.Values["id"].ToString());
  9. _commonModel = commonModelService.Find(_modelId);
  10. if (string.IsNullOrEmpty(Request.Form["ReplyContent"])) ModelState.AddModelError("ReplyContent", "必须输入回复内容!");
  11. else
  12. {
  13. _commonModel.Consultation.ReplyContent = Request.Form["ReplyContent"];
  14. _commonModel.Consultation.ReplyTime = System.DateTime.Now;
  15. _commonModel.Status = 29;
  16. commonModelService.Update(_commonModel);
  17. }
  18. }
  19. return View(_commonModel.Consultation);
  20. }

过程是:

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

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

3、返回视图

三、删除评论
在Consultation控制器,添加Delete方法

  1. /// <summary>
  2. /// 删除评论
  3. /// </summary>
  4. /// <param name="id">公共模型ID</param>
  5. /// <returns></returns>
  6. public ActionResult Delete(int id)
  7. {
  8. var _commonModel = commonModelService.Find(id);
  9. if (_commonModel == null) return Json(false);
  10. if (commonModelService.Delete(_commonModel)) return Json(true);
  11. else return Json(false);
  12. }
  13. 然后打开ManageList视图,添加删除js代码
  14. //删除
  15. function del() {
  16. var rows = $("#Consultation_List").datagrid("getSelections");
  17. if (!rows || rows.length < 1) {
  18. $.messager.alert("提示", "未选择任何行!");
  19. return;
  20. }
  21. else if (rows.length > 0) {
  22. $.messager.confirm("确认", "您确定要删除所选行吗?", function (r) {
  23. if (r) {
  24. $.messager.progress();
  25. $.each(rows, function (index, value) {
  26. $.ajax({
  27. type: "post",
  28. url: "@Url.Action("Delete", "Consultation")",
  29. data: { id: value.ModelID },
  30. async: false,
  31. success: function (data) {
  32. }
  33. });
  34. });
  35. $.messager.progress('close');
  36. //清除选择行
  37. rows.length = 0;
  38. $("#Consultation_List").datagrid('reload');
  39. }
  40. });
  41. return;
  42. }

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

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

人气教程排行