ASP.NET Core身份模型自定义选择列表 [英] ASP.NET Core Identity Model Customization Select List

查看:74
本文介绍了ASP.NET Core身份模型自定义选择列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是asp.net核心的新手(使用Core 2.1),并且正在尝试自定义身份模型.我已经弄清楚了如何添加自定义字段,例如 FirstName .但是,我一直试图在模型中添加选择列表以用于注册.列表的内容应来自数据库中的另一个表.我已经扩展了用户模型,以便数据库与另一个表具有一对多的关系,但是我不确定如何用另一个表的内容填充注册表单并将选定的值获取到用户表.我已经找到了如何使用常规控制器执行此操作的方向,但是没有找到如何将其与身份模型合并的方法.

I'm new to asp.net core (using Core 2.1) and am trying to customize the identity model. I've figured out how to add custom fields, like FirstName. However, I'm stuck on trying to add a select list to the model for use in registration. The contents of the list should come from another table in the database. I've extended the user model so that the database has a one-to-many relationship with the other table, but I'm not sure how to populate the registration form with the contents of the other table and get the selected value into the user table. I've found direction on how to do this with a regular controller, but not how to incorporate it with the identity model.

特定的情况是,当用户注册时,他们需要标识一个级别(例如,初级,高级等),该级别确定了他们在应用程序中看到的选项.

The specific scenario is that when users register they need to identify a level (eg. Junior, Senior, etc.) that determines options they see within the application.

我的用户模型是:

public class ApplicationUser : IdentityUser
{
    public ApplicationUser() : base() { }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public Level Level { get; set; }
}

我的关卡模型是:

public class Level
{
    public int Id { get; set; }

    public string LevelName { get; set; }

    public string LevelDescription { get; set; }

    public ICollection<ApplicationUser> Users { get; set; }
}

我只是不确定在哪里使用 Register.cshtml.cs Register.cshtml ,或者我是否应该考虑其他方法.

I'm just not sure where to go with Register.cshtml.cs and Register.cshtml, or if there is some other way I should be thinking about this.

我尝试过向 InputModel 添加级别,但是我不知道如何实际填充列表或获取用户模型的ID.

I've tried adding level to the InputModel, but I don't know how to actually populate the list or grab the id for my user model.

public int LevelId { get; set; }
public ICollection<SelectListItem> Levels { get; set; }

提前谢谢.

推荐答案

好的,我终于明白了.这个问题使我步入正轨: ASP.Net中的身份核心2.1:自定义AccountController .

OK, I finally figured this out. This question got me on the right track: Identity in ASP.Net Core 2.1 : Customize AccountController.

我遇到的问题的关键在于,与MVC不同,剃须刀页面的控制器是背后的代码,在本例中为Register.cshtml.cs.以下大多数内容都在该文件中.

The key to my issue was that, unlike MVC, the controller for the razor page is the code behind, in this case, Register.cshtml.cs. Most of the following is in that file.

首先,我添加了对数据库的引用,以便可以访问级别"表.我在默认文件中添加了3行上下文",以使数据库可用.

First I added a reference to the database so I could get to the Levels table. I added the 3 lines that say "context" to the default file to make the database available.

    private readonly SignInManager<ApplicationUser> _signInManager;
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly ILogger<RegisterModel> _logger;
    private readonly IEmailSender _emailSender;
    private readonly ApplicationDbContext _context;

    public RegisterModel(
        UserManager<ApplicationUser> userManager,
        SignInManager<ApplicationUser> signInManager,
        ILogger<RegisterModel> logger,
        IEmailSender emailSender,
        ApplicationDbContext context)
    {
        _userManager = userManager;
        _signInManager = signInManager;
        _logger = logger;
        _emailSender = emailSender;
        _context = context;
    }

接下来,我在模型视图中添加了一个属性来保存级别(与Input和ReturnUrl处于同一部分).

Next I added a property to the model view to hold the Levels (in the same section as Input and ReturnUrl).

public IEnumerable<SelectListItem> Levels { get; set; }

在InputModel内部,我只有LevelId,因为我将Levels移到了模型中.

Inside InputModel, I only have the LevelId because I moved Levels out to the model.

    public class InputModel
    {
     ...
     public int LevelId { get; set; }
     ...
    }

我添加了OnGet方法,以便在模型进入视图之前对其进行实际填充.

I added to the OnGet method to actually populate the model before it goes to the view.

    public void OnGet(string returnUrl = null)
    {
        ReturnUrl = returnUrl;

        // collect all levels from database
        List<Level> availableLevels = _context.Levels.ToList();
        // convert to selectlistitems
        Levels = availableLevels.Select(x => new SelectListItem() { Text = x.LevelName, Value = x.Id.ToString() }).ToList();
    }

OnPostAsync需要知道如何处理所选级别,否则它将不会进入数据库.

OnPostAsync needs to know what to do with the selected level, otherwise it won't make it into the database.

   if (ModelState.IsValid)
        {
            var user = new ApplicationUser
            {
                UserName = Input.Email,
                Email = Input.Email,
                Level = _context.Levels.Where(x => x.Id == Input.LevelId).FirstOrDefault()
            };
            ...

最后,在Register.cshtml中,我添加了此标记以在视图中显示级别.

Finally, in Register.cshtml I added this tag to display the level in the view.

       <div class="form-group">
            <label asp-for="Input.LevelId"></label>
            <select asp-for="Input.LevelId" asp-items="@(new SelectList(Model.Levels, "Value", "Text"))" class="form-control">
                <option value="">Pick One</option>
            </select>
            <span asp-validation-for="Input.LevelId" class="text-danger"></span>
        </div>

一切似乎都按预期进行了.

Everything seems to be working as intended now.

这篇关于ASP.NET Core身份模型自定义选择列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆