英文:
Razor page <input asp-for> only submits as null value for my class' int value, despite entering a value
问题
无法将整数与<input asp-for="" />
一起使用来创建新的Department
实例,当到达OnPost()
时,该值会恢复为null。我不知道问题出在哪里,<input asp-for="" />
对于Department
内的其他值类型正常工作。
Department.cs
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics.CodeAnalysis;
namespace Scratch.Data
{
public class Department
{
[Key]
public int Id { get; set; }
[Required]
public string departmentName { get; set; }
[Required]
[ForeignKey("AspNetUsers")]
public string departmentManagerId { get; set; }
[AllowNull]
[ForeignKey("AspNetUserRoles")]
public string? managerRoleId;
[AllowNull]
[ForeignKey("AspNetUserRoles")]
public string? userRoleId;
[AllowNull]
public int? defaultDisplayOrder;
}
}
我将下面的代码简化为仅测试我想要从页面中获取的一个输入:
CreateDepartmentIntTestModel
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Build.Evaluation;
using Microsoft.CodeAnalysis;
using Microsoft.EntityFrameworkCore;
using Scratch.Data;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Mvc.TagHelpers;
namespace Scratch.Pages.Admin_Tools
{
[BindProperties]
[Authorize(Roles = "Administrators,ProjectManager")]
public class CreateDepartmentIntTestModel : PageModel
{
private readonly ApplicationDbContext _db;
private readonly UserManager<IdentityUser> _userManager;
private readonly IUserStore<IdentityUser> _userStore;
private readonly RoleManager<IdentityRole> _roleManager;
private readonly IRoleStore<IdentityRole> _roleStore;
public Department Department { get; set; }
public CreateDepartmentIntTestModel(ApplicationDbContext db, UserManager<IdentityUser> userManager,
IUserStore<IdentityUser> userStore,
RoleManager<IdentityRole> roleManager,
IRoleStore<IdentityRole> roleStore)
{
_db = db;
_userManager = userManager;
_userStore = userStore;
_roleManager = roleManager;
_roleStore = roleStore;
}
public async Task<IActionResult> OnGet()
{
return Page();
}
public async Task<IActionResult> OnPost()
{
//这始终为空,尽管有输入
if (Department.defaultDisplayOrder == null)
{
//在这里添加断点:
ModelState.AddModelError("Department.defaultDisplayOrder", "显示顺序为空");
}
return Page();
}
}
}
HTML
@page
@model Scratch.Pages.Admin_Tools.CreateDepartmentIntTestModel
@{
}
<form method="post">
<input hidden asp-for="Department.managerRoleId" />
<input hidden asp-for="Department.userRoleId" />
<div class="border p-3 mt-4">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="row pb-2">
<h2 class="text-primary pl-3">
测试输入部门的数字值:
</h2>
</div>
<div class="mb-3">
部门显示顺序值:
<input asp-for="Department.defaultDisplayOrder" class="form-control" />
<span asp-validation-for="Department.defaultDisplayOrder" class="text-danger"></span>
<hr/>
</div>
<button type="submit" class="btn btn-primary" style="width:150px;">创建</button>
</div>
</form>
英文:
I cannot seem to use integers with <input asp-for="" />
when creating a new Department
instance, the value reverts to null when reaching OnPost()
. I have no idea as to what the issue is,
<input asp-for="" />
works fine for the other value types within Department
.
Department.cs
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics.CodeAnalysis;
namespace Scratch.Data
{
public class Department
{
[Key]
public int Id { get; set; }
[Required]
public string departmentName { get; set; }
[Required]
[ForeignKey("AspNetUsers")]
public string departmentManagerId { get; set; }
[AllowNull]
[ForeignKey("AspNetUserRoles")]
public string? managerRoleId;
[AllowNull]
[ForeignKey("AspNetUserRoles")]
public string? userRoleId;
[AllowNull]
public int? defaultDisplayOrder;
}
}
I simplified the code below down to testing for the one input I want to get from the page:
CreateDepartmentIntTestModel
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Build.Evaluation;
using Microsoft.CodeAnalysis;
using Microsoft.EntityFrameworkCore;
using Scratch.Data;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Mvc.TagHelpers;
namespace Scratch.Pages.Admin_Tools
{
[BindProperties]
[Authorize(Roles = "Administrators,ProjectManager")]
public class CreateDepartmentIntTestModel : PageModel
{
private readonly ApplicationDbContext _db;
private readonly UserManager<IdentityUser> _userManager;
private readonly IUserStore<IdentityUser> _userStore;
private readonly RoleManager<IdentityRole> _roleManager;
private readonly IRoleStore<IdentityRole> _roleStore;
public Department Department { get; set; }
public CreateDepartmentIntTestModel(ApplicationDbContext db, UserManager<IdentityUser> userManager,
IUserStore<IdentityUser> userStore,
RoleManager<IdentityRole> roleManager,
IRoleStore<IdentityRole> roleStore)
{
_db = db;
_userManager = userManager;
_userStore = userStore;
_roleManager = roleManager;
_roleStore = roleStore;
}
public async Task<IActionResult> OnGet()
{
return Page();
}
public async Task<IActionResult> OnPost()
{
//this is always null despite inputs
if (Department.defaultDisplayOrder == null)
{
//add breakpoint here:
ModelState.AddModelError("Department.defaultDisplayOrder", "display order is null");
}
return Page();
}
}
}
Html
@page
@model Scratch.Pages.Admin_Tools.CreateDepartmentIntTestModel
@{
}
<form method="post">
<input hidden asp-for="Department.managerRoleId" />
<input hidden asp-for="Department.userRoleId" />
<div class="border p-3 mt-4">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="row pb-2">
<h2 class="text-primary pl-3">
Test to enter a numeric value to department:
</h2>
</div>
<div class="mb-3">
Department Display Order Value:
<input asp-for="Department.defaultDisplayOrder" class="form-control" />
<span asp-validation-for="Department.defaultDisplayOrder" class="text-danger"></span>
<hr/>
</div>
<button type="submit" class="btn btn-primary" style="width:150px;">Create</button>
</div>
</form>
答案1
得分: 1
你的 defaultDisplayOrder
成员是一个字段,而不是属性(参考:https://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property)。模型绑定仅适用于公共属性。在声明中添加 { get; set; }
使其成为属性:
public int? defaultDisplayOrder { get; set; }
英文:
Your defaultDisplayOrder
member is a field, not a property (ref: https://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property). Model binding only works with public properties. Add { get; set; }
to the declaration to make it a property:
public int? defaultDisplayOrder { get; set; }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论