ASP.NET Core MVC Usermanager 帮助新手

问题描述 投票:0回答:1

我正在寻找有关我正在构建的 ASP.NET Core 应用程序的一些支持

在我的寄存器模型中,我有以下内容:

namespace DealerManager.Web.Areas.Identity.Pages.Account
{
    public class RegisterModel : PageModel
    {
        private readonly SignInManager<Employee> _signInManager;
        private readonly UserManager<Employee> _userManager;
        private readonly IUserStore<Employee> _userStore;
        private readonly IUserEmailStore<Employee> _emailStore;
        private readonly ILogger<RegisterModel> _logger;
        private readonly IEmailSender _emailSender;

        public RegisterModel(
            UserManager<Employee> userManager,
            IUserStore<Employee> userStore,
            SignInManager<Employee> signInManager,
            ILogger<RegisterModel> logger,
            IEmailSender emailSender)
        {
            _userManager = userManager;
            _userStore = userStore;
            _emailStore = GetEmailStore();
            _signInManager = signInManager;
            _logger = logger;
            _emailSender = emailSender;
        }

        /// <summary>
        ///     This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        [BindProperty]
        public InputModel Input { get; set; }

        /// <summary>
        ///     This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public string ReturnUrl { get; set; }

        /// <summary>
        ///     This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public IList<AuthenticationScheme> ExternalLogins { get; set; }
    }
}

    /// <summary>
    ///     This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
    ///     directly from your code. This API may change or be removed in future releases.
    /// </summary>
    public class InputModel
    {
        /// <summary>
        ///     This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        [Required]
        [EmailAddress]
        [Display(Name = "Email")]
        public string Email { get; set; }
        [Required]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }

        [Required]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }

        [DataType(DataType.Date)]
        [Display(Name = "Start Date")]
        public DateTime? StartDate { get; set; }

        [Required]
        [Display(Name = "Line Managers Email Address")]
        public string LineManagerEmail { get; set; }

        public string? JobTitle { get; set; }
        public string? Location { get; set; }
        public string? Franchise { get; set; }
        public string? Department { get; set; }
    }

当我创建新用户时,所有这些字段都会映射回用户表并且一切正常,我正在尝试创建一个评估模型,该模型通过我的评估控制器使用这些字段创建评估

Start Date, First Name, Last name, Job title, Location, 
Franchise, Department, Email, lineManagerEmail

基本上只需按一下按钮,我就可以查找我的用户表,并为用户表中的每个员工创建评估。

如有任何帮助,我们将不胜感激

c# asp.net-core asp.net-core-mvc
1个回答
0
投票

您的需求完全没有特殊的逻辑。这是一个简化的示例。

1.创建模型、控制器和视图。

型号

public class Appraisal
{
    public int Id { get; set; }
    public DateTime StartDate { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string JobTitle { get; set; }
    public string Location { get; set; }
    public string Franchise { get; set; }
    public string Department { get; set; }
    public string Email { get; set; }
    public string LineManagerEmail { get; set; }
    ...
}

控制器

[HttpPost]
public async Task<IActionResult> CreateAppraisals()
{
    var users = await _userManager.Users.ToListAsync();

    foreach (var user in users)
    {
        var appraisal = new Appraisal
        {
            //Replace the code with your custom appraisal rules
            StartDate = user.StartDate,
            FirstName = user.FirstName,
            LastName = user.LastName,
            JobTitle = user.JobTitle,
            Location = user.Location,
            Franchise = user.Franchise,
            Department = user.Department,
            Email = user.Email,
            LineManagerEmail = user.LineManagerEmail
        };

        _context.Appraisal.Add(appraisal);
    }

    await _context.SaveChangesAsync();

    return RedirectToAction(nameof(Index));
}

查看

@model WebApplication1.Models.Appraisal

@{
    ViewData["Title"] = "Create";
}

<h1>Create Appraisal</h1>

<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="CreateAppraisals">
            <div>
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

2.更新您的 DbContext

public DbSet<WebApplication1.Models.Appraisal> Appraisal { get; set; } = default!;

© www.soinside.com 2019 - 2024. All rights reserved.