Razor页面中的``只提交为我的类的int值为空值,尽管输入了值。

huangapple go评论101阅读模式
英文:

Razor page <input asp-for> only submits as null value for my class' int value, despite entering a value

问题

无法将整数与&lt;input asp-for=&quot;&quot; /&gt;一起使用来创建新的Department实例,当到达OnPost()时,该值会恢复为null。我不知道问题出在哪里,&lt;input asp-for=&quot;&quot; /&gt; 对于Department内的其他值类型正常工作。

Department.cs

  1. using System.ComponentModel.DataAnnotations;
  2. using System.ComponentModel.DataAnnotations.Schema;
  3. using System.Diagnostics.CodeAnalysis;
  4. namespace Scratch.Data
  5. {
  6. public class Department
  7. {
  8. [Key]
  9. public int Id { get; set; }
  10. [Required]
  11. public string departmentName { get; set; }
  12. [Required]
  13. [ForeignKey("AspNetUsers")]
  14. public string departmentManagerId { get; set; }
  15. [AllowNull]
  16. [ForeignKey("AspNetUserRoles")]
  17. public string? managerRoleId;
  18. [AllowNull]
  19. [ForeignKey("AspNetUserRoles")]
  20. public string? userRoleId;
  21. [AllowNull]
  22. public int? defaultDisplayOrder;
  23. }
  24. }

我将下面的代码简化为仅测试我想要从页面中获取的一个输入:

CreateDepartmentIntTestModel

  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Identity;
  3. using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.AspNetCore.Mvc.RazorPages;
  6. using Microsoft.Build.Evaluation;
  7. using Microsoft.CodeAnalysis;
  8. using Microsoft.EntityFrameworkCore;
  9. using Scratch.Data;
  10. using System.Text.RegularExpressions;
  11. using Microsoft.AspNetCore.Mvc.TagHelpers;
  12. namespace Scratch.Pages.Admin_Tools
  13. {
  14. [BindProperties]
  15. [Authorize(Roles = "Administrators,ProjectManager")]
  16. public class CreateDepartmentIntTestModel : PageModel
  17. {
  18. private readonly ApplicationDbContext _db;
  19. private readonly UserManager<IdentityUser> _userManager;
  20. private readonly IUserStore<IdentityUser> _userStore;
  21. private readonly RoleManager<IdentityRole> _roleManager;
  22. private readonly IRoleStore<IdentityRole> _roleStore;
  23. public Department Department { get; set; }
  24. public CreateDepartmentIntTestModel(ApplicationDbContext db, UserManager<IdentityUser> userManager,
  25. IUserStore<IdentityUser> userStore,
  26. RoleManager<IdentityRole> roleManager,
  27. IRoleStore<IdentityRole> roleStore)
  28. {
  29. _db = db;
  30. _userManager = userManager;
  31. _userStore = userStore;
  32. _roleManager = roleManager;
  33. _roleStore = roleStore;
  34. }
  35. public async Task<IActionResult> OnGet()
  36. {
  37. return Page();
  38. }
  39. public async Task<IActionResult> OnPost()
  40. {
  41. //这始终为空,尽管有输入
  42. if (Department.defaultDisplayOrder == null)
  43. {
  44. //在这里添加断点:
  45. ModelState.AddModelError("Department.defaultDisplayOrder", "显示顺序为空");
  46. }
  47. return Page();
  48. }
  49. }
  50. }

HTML

  1. @page
  2. @model Scratch.Pages.Admin_Tools.CreateDepartmentIntTestModel
  3. @{
  4. }
  5. <form method="post">
  6. <input hidden asp-for="Department.managerRoleId" />
  7. <input hidden asp-for="Department.userRoleId" />
  8. <div class="border p-3 mt-4">
  9. <div asp-validation-summary="All" class="text-danger"></div>
  10. <div class="row pb-2">
  11. <h2 class="text-primary pl-3">
  12. 测试输入部门的数字值:
  13. </h2>
  14. </div>
  15. <div class="mb-3">
  16. 部门显示顺序值:
  17. <input asp-for="Department.defaultDisplayOrder" class="form-control" />
  18. <span asp-validation-for="Department.defaultDisplayOrder" class="text-danger"></span>
  19. <hr/>
  20. </div>
  21. <button type="submit" class="btn btn-primary" style="width:150px;">创建</button>
  22. </div>
  23. </form>
英文:

I cannot seem to use integers with &lt;input asp-for=&quot;&quot; /&gt; when creating a new Department instance, the value reverts to null when reaching OnPost(). I have no idea as to what the issue is,
&lt;input asp-for=&quot;&quot; /&gt; works fine for the other value types within Department.

Department.cs

  1. using System.ComponentModel.DataAnnotations;
  2. using System.ComponentModel.DataAnnotations.Schema;
  3. using System.Diagnostics.CodeAnalysis;
  4. namespace Scratch.Data
  5. {
  6. public class Department
  7. {
  8. [Key]
  9. public int Id { get; set; }
  10. [Required]
  11. public string departmentName { get; set; }
  12. [Required]
  13. [ForeignKey(&quot;AspNetUsers&quot;)]
  14. public string departmentManagerId { get; set; }
  15. [AllowNull]
  16. [ForeignKey(&quot;AspNetUserRoles&quot;)]
  17. public string? managerRoleId;
  18. [AllowNull]
  19. [ForeignKey(&quot;AspNetUserRoles&quot;)]
  20. public string? userRoleId;
  21. [AllowNull]
  22. public int? defaultDisplayOrder;
  23. }
  24. }

I simplified the code below down to testing for the one input I want to get from the page:

CreateDepartmentIntTestModel

  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Identity;
  3. using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.AspNetCore.Mvc.RazorPages;
  6. using Microsoft.Build.Evaluation;
  7. using Microsoft.CodeAnalysis;
  8. using Microsoft.EntityFrameworkCore;
  9. using Scratch.Data;
  10. using System.Text.RegularExpressions;
  11. using Microsoft.AspNetCore.Mvc.TagHelpers;
  12. namespace Scratch.Pages.Admin_Tools
  13. {
  14. [BindProperties]
  15. [Authorize(Roles = &quot;Administrators,ProjectManager&quot;)]
  16. public class CreateDepartmentIntTestModel : PageModel
  17. {
  18. private readonly ApplicationDbContext _db;
  19. private readonly UserManager&lt;IdentityUser&gt; _userManager;
  20. private readonly IUserStore&lt;IdentityUser&gt; _userStore;
  21. private readonly RoleManager&lt;IdentityRole&gt; _roleManager;
  22. private readonly IRoleStore&lt;IdentityRole&gt; _roleStore;
  23. public Department Department { get; set; }
  24. public CreateDepartmentIntTestModel(ApplicationDbContext db, UserManager&lt;IdentityUser&gt; userManager,
  25. IUserStore&lt;IdentityUser&gt; userStore,
  26. RoleManager&lt;IdentityRole&gt; roleManager,
  27. IRoleStore&lt;IdentityRole&gt; roleStore)
  28. {
  29. _db = db;
  30. _userManager = userManager;
  31. _userStore = userStore;
  32. _roleManager = roleManager;
  33. _roleStore = roleStore;
  34. }
  35. public async Task&lt;IActionResult&gt; OnGet()
  36. {
  37. return Page();
  38. }
  39. public async Task&lt;IActionResult&gt; OnPost()
  40. {
  41. //this is always null despite inputs
  42. if (Department.defaultDisplayOrder == null)
  43. {
  44. //add breakpoint here:
  45. ModelState.AddModelError(&quot;Department.defaultDisplayOrder&quot;, &quot;display order is null&quot;);
  46. }
  47. return Page();
  48. }
  49. }
  50. }

Html

  1. @page
  2. @model Scratch.Pages.Admin_Tools.CreateDepartmentIntTestModel
  3. @{
  4. }
  5. &lt;form method=&quot;post&quot;&gt;
  6. &lt;input hidden asp-for=&quot;Department.managerRoleId&quot; /&gt;
  7. &lt;input hidden asp-for=&quot;Department.userRoleId&quot; /&gt;
  8. &lt;div class=&quot;border p-3 mt-4&quot;&gt;
  9. &lt;div asp-validation-summary=&quot;All&quot; class=&quot;text-danger&quot;&gt;&lt;/div&gt;
  10. &lt;div class=&quot;row pb-2&quot;&gt;
  11. &lt;h2 class=&quot;text-primary pl-3&quot;&gt;
  12. Test to enter a numeric value to department:
  13. &lt;/h2&gt;
  14. &lt;/div&gt;
  15. &lt;div class=&quot;mb-3&quot;&gt;
  16. Department Display Order Value:
  17. &lt;input asp-for=&quot;Department.defaultDisplayOrder&quot; class=&quot;form-control&quot; /&gt;
  18. &lt;span asp-validation-for=&quot;Department.defaultDisplayOrder&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
  19. &lt;hr/&gt;
  20. &lt;/div&gt;
  21. &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot; style=&quot;width:150px;&quot;&gt;Create&lt;/button&gt;
  22. &lt;/div&gt;
  23. &lt;/form&gt;

答案1

得分: 1

你的 defaultDisplayOrder 成员是一个字段,而不是属性(参考:https://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property)。模型绑定仅适用于公共属性。在声明中添加 { get; set; } 使其成为属性:

  1. 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:

  1. public int? defaultDisplayOrder { get; set; }

huangapple
  • 本文由 发表于 2023年2月19日 05:53:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定