ABP学习入门系列(展示实现增删改查)
Posted 田家小白菜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ABP学习入门系列(展示实现增删改查)相关的知识,希望对你有一定的参考价值。
大致要实现的 效果如下
1,添加Controller(用到的X.PagedList 注意到nuget添加)
using System.Web.Mvc; using Abp.Application.Services.Dto; using Abp.Runtime.Caching; using Abp.Threading; using Abp.Web.Mvc.Authorization; using AutoMapper; using LearningMpaAbp.Notifications; using LearningMpaAbp.Tasks; using LearningMpaAbp.Tasks.Dtos; using LearningMpaAbp.Users; using LearningMpaAbp.Users.Dto; using LearningMpaAbp.Web.Models.Tasks; using X.PagedList; namespace LearningMpaAbp.Web.Controllers { [AbpMvcAuthorize] public class TasksController : LearningMpaAbpControllerBase { private readonly ITaskAppService _taskAppService; private readonly IUserAppService _userAppService; private readonly INotificationAppService _notificationAppService; private readonly ICacheManager _cacheManager; public TasksController(ITaskAppService taskAppService, IUserAppService userAppService, ICacheManager cacheManager, INotificationAppService notificationAppService) { _taskAppService = taskAppService; _userAppService = userAppService; _cacheManager = cacheManager; _notificationAppService = notificationAppService; } public ActionResult Index(GetTasksInput input) { var output = _taskAppService.GetTasks(input); var model = new IndexViewModel(output.Tasks) { SelectedTaskState = input.State }; return View(model); } // GET: Tasks public ActionResult PagedList(int? page) { //每页行数 var pageSize = 5; var pageNumber = page ?? 1; //第几页 var filter = new GetTasksInput { SkipCount = (pageNumber - 1) * pageSize, //忽略个数 MaxResultCount = pageSize }; var result = _taskAppService.GetPagedTasks(filter); //已经在应用服务层手动完成了分页逻辑,所以需手动构造分页结果 var onePageOfTasks = new StaticPagedList<TaskDto>(result.Items, pageNumber, pageSize, result.TotalCount); //将分页结果放入ViewBag供View使用 ViewBag.OnePageOfTasks = onePageOfTasks; return View(); } public PartialViewResult GetList(GetTasksInput input) { var output = _taskAppService.GetTasks(input); return PartialView("_List", output.Tasks); } /// <summary> /// 获取创建任务分部视图 /// 该方法使用ICacheManager进行缓存,在WebModule中配置缓存过期时间为10mins /// </summary> /// <returns></returns> public PartialViewResult RemoteCreate() { //1.1 注释该段代码,使用下面缓存的方式 //var userList = _userAppService.GetUsers(); //1.2 同步调用异步解决方案(最新Abp创建的模板项目已经去掉该同步方法,所以可以通过下面这种方式获取用户列表) //var userList = AsyncHelper.RunSync(() => _userAppService.GetUsersAsync()); //1.3 缓存版本 var userList = _cacheManager.GetCache("ControllerCache").Get("AllUsers", () => _userAppService.GetUsers()); //1.4 转换为泛型版本 //var userList = _cacheManager.GetCache("ControllerCache").AsTyped<string, ListResultDto<UserListDto>>().Get("AllUsers", () => _userAppService.GetUsers()); //1.5 泛型缓存版本 //var userList = _cacheManager.GetCache<string, ListResultDto<UserListDto>>("ControllerCache").Get("AllUsers", () => _userAppService.GetUsers()); ViewBag.AssignedPersonId = new SelectList(userList.Items, "Id", "Name"); return PartialView("_CreateTaskPartial"); } /// <summary> /// 获取创建任务分部视图(子视图) /// 该方法使用[OutputCache]进行缓存,缓存过期时间为2mins /// </summary> /// <returns></returns> [ChildActionOnly] [OutputCache(Duration = 1200, VaryByParam = "none")] public PartialViewResult Create() { var userList = _userAppService.GetUsers(); ViewBag.AssignedPersonId = new SelectList(userList.Items, "Id", "Name"); return PartialView("_CreateTask"); } // POST: Tasks/Create // 为了防止“过多发布”攻击,请启用要绑定到的特定属性,有关 // 详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(CreateTaskInput task) { var id = _taskAppService.CreateTask(task); var input = new GetTasksInput(); var output = _taskAppService.GetTasks(input); return PartialView("_List", output.Tasks); } // GET: Tasks/Edit/5 public PartialViewResult Edit(int id) { var task = _taskAppService.GetTaskById(id); var updateTaskDto = Mapper.Map<UpdateTaskInput>(task); var userList = _userAppService.GetUsers(); ViewBag.AssignedPersonId = new SelectList(userList.Items, "Id", "Name", updateTaskDto.AssignedPersonId); return PartialView("_EditTask", updateTaskDto); } // POST: Tasks/Edit/5 // 为了防止“过多发布”攻击,请启用要绑定到的特定属性,有关 // 详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit(UpdateTaskInput updateTaskDto) { _taskAppService.UpdateTask(updateTaskDto); var input = new GetTasksInput(); var output = _taskAppService.GetTasks(input); return PartialView("_List", output.Tasks); } public ActionResult NotifyUser() { _notificationAppService.NotificationUsersWhoHaveOpenTask(); return new EmptyResult(); } } }
2,视图
@using Abp.Web.Mvc.Extensions @model LearningMpaAbp.Web.Models.Tasks.IndexViewModel @{ ViewBag.Title = L("TaskList"); ViewBag.ActiveMenu = "TaskList"; //Matches with the menu name in SimpleTaskAppNavigationProvider to highlight the menu item } @section scripts{ @html.IncludeScript("~/Views/Tasks/index.js"); } <h2> @L("TaskList") <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#add">Create Task</button> <a class="btn btn-primary" data-toggle="modal" href="@Url.Action("RemoteCreate")" data-target="#modal" role="button">(Create Task)使用Remote方式调用Modal进行展现</a> <a class="btn btn-success" href="@Url.Action("PagedList")" role="button">分页展示</a> <button type="button" class="btn btn-info" onclick="notifyUser();">通知未完成任务的用户</button> <!--任务清单按照状态过滤的下拉框--> <span class="pull-right"> @Html.DropDownListFor( model => model.SelectedTaskState, Model.GetTaskStateSelectListItems(), new { @class = "form-control select2", id = "TaskStateCombobox" }) </span> </h2> <!--任务清单展示--> <div class="row" id="taskList"> @{ Html.RenderPartial("_List", Model.Tasks); } </div> <!--通过初始加载页面的时候提前将创建任务模态框加载进来--> @Html.Action("Create") <!--编辑任务模态框通过ajax动态填充到此div中--> <div id="edit"> </div> <!--Remote方式弹出创建任务模态框--> <div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="createTask" data-backdrop="static"> <div class="modal-dialog" role="document"> <div class="modal-content"> </div> </div> </div>
另外还有_createTaskPartial,_EditTaskPartial 等,这里就不贴代码了
以上。。。
参考http://www.jianshu.com/p/620c20fa511b
代码地址https://github.com/tianxiangd/LearnAbp
以上是关于ABP学习入门系列(展示实现增删改查)的主要内容,如果未能解决你的问题,请参考以下文章
ABP教程- 开始一个简单的任务管理系统 - 实现UI端的增删改查