【发布时间】:2020-06-07 06:43:50
【问题描述】:
我尝试通过为用户设置角色来创建用户。
角色属性是用户类中的一个必填字段。
问题是当我做了一个标记以从角色表中检索数据以便能够将角色设置给用户时,插入查询不起作用。用户表具有属性“roleId”,但用户类具有角色类的“角色”属性。
我会将 UserCreate Razor 页面的代码与我使用的模型放在一起。
UserCreate.cshtml:
@page
@model SprintOneBack.UserCreateModel
@{
ViewData["Title"] = "UserCreate";
}
<h1>Create</h1>
<h4>User</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="User.Nom" class="control-label"></label>
<input asp-for="User.Nom" class="form-control" />
<span asp-validation-for="User.Nom" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="User.Email" class="control-label"></label>
<input asp-for="User.Email" class="form-control" />
<span asp-validation-for="User.Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="User.password" class="control-label"></label>
<input asp-for="User.password" class="form-control" />
<span asp-validation-for="User.password" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="User.role" class="control-label"></label>
<select asp-for="User.role" class="form-control" asp-items="@(new SelectList(Model.displayRoles, "Id", "Label"))">
<option value="" selected disabled>-- select Role --</option>
</select>
@*<input asp-for="User.role.Label" class="form-control" />*@
<span asp-validation-for="User.role" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-page="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
UserCreateModel.cshtml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using SprintOneBack.Model;
namespace SprintOneBack
{
public class UserCreateModel : PageModel
{
private readonly ApplicationDbContext _context;
public UserCreateModel(ApplicationDbContext context)
{
_context = context;
displayRoles = _context.Role.ToList();
}
public IActionResult OnGet()
{
return Page();
}
public IEnumerable<Role> displayRoles { get; set; }
public async Task OnGetRoles()
{
displayRoles = await _context.Role.ToListAsync();
}
[BindProperty]
public User User { get; set; }
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
_context.User.Add(User);
await _context.SaveChangesAsync();
return RedirectToPage("./UserIndex");
}
}
}
用户类:
public class User
{
[Key]
public int Id { get; set; }
[Required]
public string Nom { get; set; }
[Required]
public string Email { get; set; }
[Required]
public string password { get; set; }
[Required]
public Role role { get; set; }
}
角色类:
public class Role
{
[Key]
public int Id { get; set; }
[Required]
public string Label { get; set; }
}
用户表和角色表:
【问题讨论】:
-
通常情况下,如果用户可以拥有多个角色,而不是一个角色...类似于多对多关系。
标签: c# razor-pages asp.net-core-3.0 razor-pages-crud