0

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>
DED
  • 391
  • 2
  • 12
  • 1
    You would need to include getter and setter on your `defaultDisplayOrder` [`property`](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties) which is currently defined as [`Fields`](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/fields); Thus, the value has not set directly from ourside and you are getting null value. Just adding `{ get; set; }` would resolve your issue. – Md Farid Uddin Kiron Feb 20 '23 at 08:43

1 Answers1

1

Your defaultDisplayOrder member is a field, not a property (ref: 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; }
Mike Brind
  • 28,238
  • 6
  • 56
  • 88