欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-16 13:41:46
一、管理列表 跟上次我的列表相似,直接贴代码了。 首先打开consultation控制器,添加managelist方法 /// ...

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

首先打开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>

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

二、回复评论
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、返回视图

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

三、删除评论
在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区域终于写完,希望对大家有所帮助。