无法从数据库aspnetboilerplate检索数据

时间:2018-11-05 12:37:48

标签: c# aspnetboilerplate

我正在使用ASP.NET Boilerplate框架来做一个小项目。我的问题是我无法从数据库中检索特定数据。

因此在Users文件夹内的DTO文件夹中,具有3个类,分别是CreateUserDTO.cs,UpdateUserDTO.cs和UserDTO.cs。 我已经声明了我的对象public int RecyclePoints {get; set;} 在所有三个中。

注意:在默认迁移中,当我添加对象并更新迁移时,没有RecyclePoints对象。

当我在应用程序中编辑和更改值时,它不会更新到数据库。我还尝试在数据库中进行编辑,并检查它是否可以检索数据但没有运气。

谢谢。

编辑1:我忘了提它不只显示RecyclePoints数据。其余的正常。

编辑2:这是UserDTO.cs代码(另一个dto具有相同的内容。)

using System;
using System.ComponentModel.DataAnnotations;
using Abp.Application.Services.Dto;
using Abp.Authorization.Users;
using Abp.AutoMapper;
using RecycleHub.Authorization.Users;

namespace RecycleHub.Users.Dto
{
    [AutoMapFrom(typeof(User))]
    public class UserDto : EntityDto<long>
    {
        [Required]
        [StringLength(AbpUserBase.MaxUserNameLength)]
        public string UserName { get; set; }

        [Required]
        [StringLength(AbpUserBase.MaxNameLength)]
        public string Name { get; set; }

        [Required]
        [StringLength(AbpUserBase.MaxSurnameLength)]
        public string Surname { get; set; }

        [Required]
        [EmailAddress]
        [StringLength(AbpUserBase.MaxEmailAddressLength)]
        public string EmailAddress { get; set; }

        public bool IsActive { get; set; }

        public string FullName { get; set; }

        public DateTime? LastLoginTime { get; set; }

        public DateTime CreationTime { get; set; }

        public int RecyclePoints { get; set; }

        public string[] Roles { get; set; }

    }
}

这是User.cs代码

using System;
using Abp.Authorization.Users;
using Abp.Extensions;
using Microsoft.AspNet.Identity;


namespace RecycleHub.Authorization.Users
{
    public class User : AbpUser<User>
    {

        public const string DefaultPassword = "123qwe";

        public static string CreateRandomPassword()
        {
            return Guid.NewGuid().ToString("N").Truncate(16);
        }

        public static User CreateTenantAdminUser(int tenantId, string emailAddress, string password)
        {
            var user = new User
            {
                TenantId = tenantId,
                UserName = AdminUserName,
                Name = AdminUserName,
                Surname = AdminUserName,
                EmailAddress = emailAddress,

                Password = new PasswordHasher().HashPassword(password)
            };

            return user;
        }
    }
}

编辑3:错过了数据服务部分。这是代码UserAppService.cs。

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Authorization.Users;
using Abp.Domain.Repositories;
using Abp.IdentityFramework;
using RecycleHub.Authorization;
using RecycleHub.Authorization.Roles;
using RecycleHub.Authorization.Users;
using RecycleHub.Roles.Dto;
using RecycleHub.Users.Dto;
using Microsoft.AspNet.Identity;

namespace RecycleHub.Users
{
    [AbpAuthorize(PermissionNames.Pages_Users)]
    public class UserAppService : AsyncCrudAppService<User, UserDto, long, PagedResultRequestDto, CreateUserDto, UpdateUserDto>, IUserAppService
    {
        private readonly UserManager _userManager;
        private readonly RoleManager _roleManager;
        private readonly IRepository<Role> _roleRepository;

        public UserAppService(
            IRepository<User, long> repository, 
            UserManager userManager, 
            IRepository<Role> roleRepository, 
            RoleManager roleManager)
            : base(repository)
        {
            _userManager = userManager;
            _roleRepository = roleRepository;
            _roleManager = roleManager;
        }

        public override async Task<UserDto> Get(EntityDto<long> input)
        {
            var user = await base.Get(input);
            var userRoles = await _userManager.GetRolesAsync(user.Id);
            user.Roles = userRoles.Select(ur => ur).ToArray();
            return user;
        }

        public override async Task<UserDto> Create(CreateUserDto input)
        {
            CheckCreatePermission();

            var user = ObjectMapper.Map<User>(input);

            user.TenantId = AbpSession.TenantId;
            user.Password = new PasswordHasher().HashPassword(input.Password);
            user.IsEmailConfirmed = true;

            //Assign roles
            user.Roles = new Collection<UserRole>();
            foreach (var roleName in input.RoleNames)
            {
                var role = await _roleManager.GetRoleByNameAsync(roleName);
                user.Roles.Add(new UserRole(AbpSession.TenantId, user.Id, role.Id));
            }

            CheckErrors(await _userManager.CreateAsync(user));

            await CurrentUnitOfWork.SaveChangesAsync();

            return MapToEntityDto(user);
        }

        public override async Task<UserDto> Update(UpdateUserDto input)
        {
            CheckUpdatePermission();

            var user = await _userManager.GetUserByIdAsync(input.Id);

            MapToEntity(input, user);

            CheckErrors(await _userManager.UpdateAsync(user));

            if (input.RoleNames != null)
            {
                CheckErrors(await _userManager.SetRoles(user, input.RoleNames));
            }

            return await Get(input);
        }

        public override async Task Delete(EntityDto<long> input)
        {
            var user = await _userManager.GetUserByIdAsync(input.Id);
            await _userManager.DeleteAsync(user);
        }

        public async Task<ListResultDto<RoleDto>> GetRoles()
        {
            var roles = await _roleRepository.GetAllListAsync();
            return new ListResultDto<RoleDto>(ObjectMapper.Map<List<RoleDto>>(roles));
        }

        protected override User MapToEntity(CreateUserDto createInput)
        {
            var user = ObjectMapper.Map<User>(createInput);
            return user;
        }

        protected override void MapToEntity(UpdateUserDto input, User user)
        {
            ObjectMapper.Map(input, user);
        }

        protected override IQueryable<User> CreateFilteredQuery(PagedResultRequestDto input)
        {
            return Repository.GetAllIncluding(x => x.Roles);
        }

        protected override async Task<User> GetEntityByIdAsync(long id)
        {
            var user = Repository.GetAllIncluding(x => x.Roles).FirstOrDefault(x => x.Id == id);
            return await Task.FromResult(user);
        }

        protected override IQueryable<User> ApplySorting(IQueryable<User> query, PagedResultRequestDto input)
        {
            return query.OrderBy(r => r.UserName);
        }

        protected virtual void CheckErrors(IdentityResult identityResult)
        {
            identityResult.CheckErrors(LocalizationManager);
        }
    }
}

IUserAppService.cs的代码

using System.Threading.Tasks;
using Abp.Application.Services;
using Abp.Application.Services.Dto;
using RecycleHub.Roles.Dto;
using RecycleHub.Users.Dto;

namespace RecycleHub.Users
{
    public interface IUserAppService : IAsyncCrudAppService<UserDto, long, PagedResultRequestDto, CreateUserDto, UpdateUserDto>
    {
        Task<ListResultDto<RoleDto>> GetRoles();
    }
}

UsersController.cs的代码

using System;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;
using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Dependency;
using Abp.Events.Bus.Exceptions;
using Abp.Events.Bus.Handlers;
using Abp.UI;
using Abp.Web.Mvc.Authorization;
using RecycleHub.Authorization;
using RecycleHub.Authorization.Roles;
using RecycleHub.Users;
using RecycleHub.Web.Models.Users;

namespace RecycleHub.Web.Controllers
{
    [AbpMvcAuthorize(PermissionNames.Pages_Users)]
    public class UsersController : RecycleHubControllerBase
    {
        private readonly IUserAppService _userAppService;
        private readonly RoleManager _roleManager;

        public UsersController(IUserAppService userAppService, RoleManager roleManager)
        {
            _userAppService = userAppService;
            _roleManager = roleManager;
        }

        public async Task<ActionResult> Index()
        {
            var users = (await _userAppService.GetAll(new PagedResultRequestDto { MaxResultCount = int.MaxValue })).Items; //Paging not implemented yet
            var roles = (await _userAppService.GetRoles()).Items;
            var model = new UserListViewModel
            {
                Users = users,
                Roles = roles
            };

            return View(model);
        }

        public async Task<ActionResult> EditUserModal(long userId)
        {
            var user = await _userAppService.Get(new EntityDto<long>(userId));
            var roles = (await _userAppService.GetRoles()).Items;
            var model = new EditUserModalViewModel
            {
                User = user,
                Roles = roles
            };
            return View("_EditUserModal", model);
        }
    }
}

这是用于显示数据的代码

@using Abp.Authorization.Users
@using Abp.Web.Mvc.Extensions
@model RecycleHub.Web.Models.Users.UserListViewModel
@{
    ViewBag.ActiveMenu = PageNames.Users; //The menu item will be active for this page.
}
@section scripts
{
    @Html.IncludeScript("~/Views/Users/Index.js")
}
<div class="row clearfix">
    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
        <div class="card">
            <div class="header">
                <h2>
                    @L("Users")
                </h2>
                <ul class="header-dropdown m-r--5">
                    <li class="dropdown">
                        <a href="javascript:void(0);" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
                            <i class="material-icons">more_vert</i>
                        </a>
                        <ul class="dropdown-menu pull-right">
                            <li><a id="RefreshButton" href="javascript:void(0);" class="waves-effect waves-block"><i class="material-icons">refresh</i>Refresh</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
            <div class="body table-responsive">
                <table class="table">
                    <thead>
                    <tr>
                        <th>@L("UserName")</th>
                        <th>@L("FullName")</th>
                        <th>@L("RecyclePoints")</th>
                        <th>@L("EmailAddress")</th>
                        <th>@L("IsActive")</th>
                        <th>@L("Actions")</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach (var user in Model.Users)
                    {
                        <tr>
                            <td>@user.UserName</td>
                            <td>@user.FullName</td>
                            <td>@user.RecyclePoints</td>
                            <td>@user.EmailAddress</td>
                            <td><i class="material-icons" style="color:@(user.IsActive ? "green":"red");">@(user.IsActive ? "check_box" : "indeterminate_check_box")</i></td>
                            <td class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
                                    <i class="material-icons">menu</i>
                                </a>
                                <ul class="dropdown-menu pull-right">
                                    <li><a href="#" class="waves-effect waves-block edit-user" data-user-id="@user.Id" data-toggle="modal" data-target="#UserEditModal"><i class="material-icons">edit</i>@L("Edit")</a></li>
                                    <li><a href="#" class="waves-effect waves-block delete-user" data-user-id="@user.Id" data-user-name="@user.UserName"><i class="material-icons">delete_sweep</i>@L("Delete")</a></li>
                                </ul>
                            </td>
                        </tr>
                    }
                    </tbody>
                </table>
                <button type="button" class="btn btn-primary btn-circle waves-effect waves-circle waves-float pull-right" data-toggle="modal" data-target="#UserCreateModal">
                    <i class="material-icons">add</i>
                </button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="UserCreateModal" tabindex="-1" role="dialog" aria-labelledby="UserCreateModalLabel" data-backdrop="static">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">
                    <span>@L("CreateNewUser")</span>
                </h4>
            </div>
            <div class="modal-body">
                <form name="userCreateForm" role="form" novalidate class="form-validation">
                    <ul class="nav nav-tabs tab-nav-right" role="tablist">
                        <li role="presentation" class="active"><a href="#create-user-details" data-toggle="tab">User Details</a></li>
                        <li role="presentation"><a href="#create-user-roles" data-toggle="tab">User Roles</a></li>
                    </ul>
                    <div class="tab-content">
                        <div role="tabpanel" class="tab-pane animated fadeIn active" id="create-user-details">
                            <div class="row clearfix" style="margin-top:10px;">
                                <div class="col-sm-12">
                                    <div class="form-group form-float">
                                        <div class="form-line">
                                            <input class="form-control" type="text" name="UserName" required maxlength="@AbpUserBase.MaxUserNameLength" minlength="2">
                                            <label class="form-label">@L("UserName")</label>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row clearfix">
                                <div class="col-sm-6">
                                    <div class="form-group form-float">
                                        <div class="form-line">
                                            <input type="text" name="Name" class="form-control" required maxlength="@AbpUserBase.MaxNameLength">
                                            <label class="form-label">@L("Name")</label>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <div class="form-group form-float">
                                        <div class="form-line">
                                            <input type="text" name="Surname" class="form-control" required maxlength="@AbpUserBase.MaxSurnameLength">
                                            <label class="form-label">@L("Surname")</label>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <div class="form-group form-float">
                                        <div class="form-line">
                                            <input type="number" name="RecyclePoints" class="form-control">
                                            <label class="form-label">@L("RecyclePoints")</label>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row clearfix">
                                <div class="col-sm-12">
                                    <div class="form-group form-float">
                                        <div class="form-line">
                                            <input type="email" name="EmailAddress" class="form-control" required maxlength="@AbpUserBase.MaxEmailAddressLength">
                                            <label class="form-label">@L("EmailAddress")</label>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row clearfix">
                                <div class="col-sm-12">
                                    <div class="form-group form-float">
                                        <div class="form-line">
                                            <input type="password" id="Password" name="Password" class="form-control" required maxlength="@AbpUserBase.MaxPlainPasswordLength">
                                            <label class="form-label">@L("Password")</label>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row clearfix">
                                <div class="col-sm-12">
                                    <div class="form-group form-float">
                                        <div class="form-line">
                                            <input type="password" id="ConfirmPassword" name="ConfirmPassword" class="form-control" required maxlength="@AbpUserBase.MaxPlainPasswordLength">
                                            <label class="form-label">@L("ConfirmPassword")</label>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row clearfix">
                                <div class="col-sm-12">
                                    <div class="checkbox">
                                        <input type="checkbox" name="IsActive" value="true" id="CreateUserIsActive" class="filled-in" checked />
                                        <label for="CreateUserIsActive">@L("IsActive")</label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div role="tabpanel" class="tab-pane animated fadeIn" id="create-user-roles">
                            <div class="row">
                                <div class="col-sm-12 ">
                                    @foreach (var role in Model.Roles)
                                    {
                                        <div class="col-sm-6">
                                            <input type="checkbox" name="role" data-role-name="@role.Name" title="@role.Description" class="filled-in" id="role-@role.Id" />
                                            <label for="role-@role.Id" title="@role.DisplayName">@role.Name</label>
                                        </div>
                                    }
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">@L("Cancel")</button>
                        <button type="submit" class="btn btn-primary waves-effect">@L("Save")</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="UserEditModal" tabindex="-1" role="dialog" aria-labelledby="UserEditModalLabel" data-backdrop="static">
    <div class="modal-dialog" role="document">
        <div class="modal-content">

        </div>
    </div>
</div>

_EditUserModal.cshtml的代码

@using RecycleHub.Web.Models.Common.Modals
@model RecycleHub.Web.Models.Users.EditUserModalViewModel
@{
    Layout = null;
}
@Html.Partial("~/Views/Shared/Modals/_ModalHeader.cshtml", new ModalHeaderViewModel(L("EditUser")))

<div class="modal-body">
    <form name="UserEditForm" role="form" novalidate class="form-validation">
        <input type="hidden" name="Id" value="@Model.User.Id" />
        <ul class="nav nav-tabs tab-nav-right" role="tablist">
            <li role="presentation" class="active"><a href="#edit-user-details" data-toggle="tab">User Details</a></li>
            <li role="presentation"><a href="#edit-user-roles" data-toggle="tab">User Roles</a></li>
        </ul>
        <div class="tab-content">
            <div role="tabpanel" class="tab-pane animated fadeIn active" id="edit-user-details">

                <div class="row clearfix" style="margin-top:10px;">
                    <div class="col-sm-12">
                        <div class="form-group form-float">
                            <div class="form-line">
                                <input id="username" type="text" name="UserName" value="@Model.User.UserName" required maxlength="32" minlength="2" class="validate form-control">
                                <label for="username" class="form-label">@L("UserName")</label>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row clearfix">
                    <div class="col-sm-6">
                        <div class="form-group form-float">
                            <div class="form-line">
                                <input id="name" type="text" name="Name" value="@Model.User.Name" required maxlength="32" class="validate form-control">
                                <label for="name" class="form-label">@L("Name")</label>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group form-float">
                            <div class="form-line">
                                <input id="surname" type="text" name="Surname" value="@Model.User.Surname" required maxlength="32" class="validate form-control">
                                <label for="surname" class="form-label">@L("Surname")</label>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group form-float">
                            <div class="form-line">
                                <input id="RecyclePoints" type="number" name="Recycle Points" value="@Model.User.RecyclePoints" class="validate form-control">
                                <label for="RecyclePoints" class="form-label">@L("RecyclePoints")</label>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row clearfix">
                    <div class="col-sm-12">
                        <div class="form-group form-float">
                            <div class="form-line">
                                <input id="email" type="email" name="EmailAddress" value="@Model.User.EmailAddress" maxlength="256" class="validate form-control">
                                <label for="email" class="form-label">@L("EmailAddress")</label>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row clearfix">
                    <div class="col-sm-12">
                        <div class="form-group form-float">
                            <div class="">
                                <input id="IsActive" type="checkbox" name="IsActive" value="true" @(Model.User.IsActive ? "checked" : "") class="form-control" />
                                <label for="IsActive" class="form-label">@L("IsActive")</label>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div role="tabpanel" class="tab-pane animated fadeIn" id="edit-user-roles">
                <div class="row">
                    <div class="col-sm-12 ">
                        @foreach (var role in Model.Roles)
                        {
                            <div class="col-sm-6">
                                <input type="checkbox" name="role" data-role-name="@role.Name" title="@role.Description" class="filled-in" id="@string.Format("role{0}",role.Id)" @(Model.UserIsInRole(role) ? "checked" : "") />
                                <label for="@string.Format("role{0}",role.Id)" title="@role.DisplayName">@role.Name</label>
                            </div>
                        }
                    </div>
                </div>
            </div>

        </div>
    </form>
</div>

@Html.Partial("~/Views/Shared/Modals/_ModalFooterWithSaveAndCancel.cshtml")

<script src="~/Views/Users/_EditUserModal.js"></script>

0 个答案:

没有答案