ABP學習入門系列(五)(展示實現增刪改查)

来源:http://www.cnblogs.com/tianjiaxiaobaicai/archive/2017/11/14/7833764.html
-Advertisement-
Play Games

大致要實現的 效果如下 1,添加Controller(用到的X.PagedList 註意到nuget添加) 2,視圖 另外還有_createTaskPartial,_EditTaskPartial 等,這裡就不貼代碼了 以上。。。 參考http://www.jianshu.com/p/620c20f ...


大致要實現的 效果如下

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

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 電商平臺和傳統店鋪相比,確實方便不少,直接在網上下單,快遞直接送貨到家。這其中,做電商平臺的童鞋表示壓力很大,因為可能同時開很多店鋪,每個店鋪都要登錄、查看訂單量、發貨揀貨等,非常辛苦。 多店鋪同時操作,需要打開很多瀏覽器,每個店鋪的帳號和密碼也要記清楚,腦子一定要夠用,不然就會頭暈腦漲。 ...
  • 相關博文: "Ubuntu 簡單安裝 Docker" "Mac OS、Ubuntu 安裝及使用 Consul" "Consul 服務註冊與服務發現" "Fabio 安裝和簡單使用" 閱讀目錄: Docker 運行 Consul 環境 Docker 運行 Fabio 環境 使用 Consul 註冊 A ...
  • MongDB .Net工具庫MongoRepository的簡單使用 最近研究了一下MongoDB資料庫,並使用了開源的在.net環境下的一個類庫,Mongo倉庫。對於數據的一些簡單的操作非常好用,特記錄供後期參考。 具體的使用過程如下: 一、新建項目,在Nuget上獲取庫。 二、在配置文件中設置數 ...
  • HAL(Hypertext Application Language,超文本應用語言)是一種RESTful API的數據格式風格,為RESTful API的設計提供了介面規範,同時也降低了客戶端與服務端介面的耦合度。很多當今流行的RESTful API開發框架,包括Spring REST,也都預設支 ...
  • 1.1 概述 c#程式開發中,資料庫操作無疑是舉足輕重的,資料庫部分的技術點可能占整個c#技術點的1/4。這幾天我一直在研究System.Data.OracleClient.dll反編譯之後的.CS,放棄c#的心都有了,底層代碼不僅全是英文註釋,而且有很多東西看都看不懂,讓我深刻體會封裝的重要性!此 ...
  • 今天是第一次接觸C#,由於長時間的做Java開發,突然轉到C#非常的不自然,但是也有了一些收穫,給大家分享一下 ...
  • "sweetalert": "^1.1.3",記住這個參數,Abp中這個組件只能用這個版本,高於它的message會沒有效果,切記 ...
  • 回到目錄 進行dotnetcore之後,各種對象都是基於DI進行生產的,這就有了對象的生命周期一說,早在autofac里也有相關知識點,這與Microsoft.Extensions.DependencyInjection是完全溫和的,方便大家理解,在講今天的組件化之前,先對DI的三種生命周期進行理解 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...