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

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

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

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 &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

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(&quot;AspNetUsers&quot;)]
		public string departmentManagerId { get; set; }

		[AllowNull]
		[ForeignKey(&quot;AspNetUserRoles&quot;)]
		public string? managerRoleId;

		[AllowNull]
		[ForeignKey(&quot;AspNetUserRoles&quot;)]
		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 = &quot;Administrators,ProjectManager&quot;)]
	public class CreateDepartmentIntTestModel : PageModel
	{
		private readonly ApplicationDbContext _db;
		

		private readonly UserManager&lt;IdentityUser&gt; _userManager;
		private readonly IUserStore&lt;IdentityUser&gt; _userStore;

		private readonly RoleManager&lt;IdentityRole&gt; _roleManager;
		private readonly IRoleStore&lt;IdentityRole&gt; _roleStore;

		public Department Department { get; set; }


		public CreateDepartmentIntTestModel(ApplicationDbContext db, UserManager&lt;IdentityUser&gt; userManager,
			IUserStore&lt;IdentityUser&gt; userStore,
			RoleManager&lt;IdentityRole&gt; roleManager,
			IRoleStore&lt;IdentityRole&gt; roleStore)
		{
			_db = db;
			_userManager = userManager;
			_userStore = userStore;
			_roleManager = roleManager;
			_roleStore = roleStore;

		}
	

		

		

		public async Task&lt;IActionResult&gt; OnGet()
		{
			
			return Page();

        }

		

		public async Task&lt;IActionResult&gt; OnPost()
		{

			
			//this is always null despite inputs
			if (Department.defaultDisplayOrder == null)
			{
				//add breakpoint here:
				ModelState.AddModelError(&quot;Department.defaultDisplayOrder&quot;, &quot;display order is null&quot;);
			}


			

			

			return Page();
		}


		

	}
}

Html

@page
@model Scratch.Pages.Admin_Tools.CreateDepartmentIntTestModel
@{
}

&lt;form method=&quot;post&quot;&gt;
	
	&lt;input hidden asp-for=&quot;Department.managerRoleId&quot;   /&gt;
	&lt;input hidden asp-for=&quot;Department.userRoleId&quot;  /&gt;

	&lt;div class=&quot;border p-3 mt-4&quot;&gt;
		&lt;div asp-validation-summary=&quot;All&quot; class=&quot;text-danger&quot;&gt;&lt;/div&gt;
		&lt;div class=&quot;row pb-2&quot;&gt;
			&lt;h2 class=&quot;text-primary pl-3&quot;&gt;
				Test to enter a numeric value to department:
			&lt;/h2&gt;
		&lt;/div&gt;
		&lt;div class=&quot;mb-3&quot;&gt;

			Department Display Order Value:
			&lt;input asp-for=&quot;Department.defaultDisplayOrder&quot; class=&quot;form-control&quot; /&gt;
			&lt;span asp-validation-for=&quot;Department.defaultDisplayOrder&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
			
			&lt;hr/&gt;
			

		&lt;/div&gt;
		&lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot; style=&quot;width:150px;&quot;&gt;Create&lt;/button&gt;

	&lt;/div&gt;
	
&lt;/form&gt;

答案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; }

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:

确定