Home > OS >  I am getting ModelState error "The id field is required." But Id field is already entered
I am getting ModelState error "The id field is required." But Id field is already entered

Time:09-26

This razor Page Create new Continent and if an ID is passed in OnGetAsync method

This EditContinent.cshtml.cs will just edit the given id continent. Below is the Model, in its OnPost method is showing error only when i try to Add new continent.

It is updating just fine which is in the else statement.

This error is showing

{The id field is required.}

I am using sqlite database

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using World.Data;
using World.Models;
using Microsoft.EntityFrameworkCore;

namespace World.Pages
{
    public class EditContinentModel : PageModel
    {
        private readonly ContinentContext _context;
        
        public EditContinentModel(ContinentContext context)
        {
            _context = context;
        }

        [BindProperty]
        public Continent Continent { get; set; }

        public async Task<IActionResult> OnGetAsync(string id)
        {
            if (id == null)
            {
                Continent = new Continent();
            }
            else
            {
                Continent = await _context.Continents.FindAsync(id);

                if (Continent == null)
                {
                    return NotFound();
                }
            }
            return Page();
        }
        public async Task<IActionResult> OnPostAsync(string id)
        {
            if (!ModelState.IsValid)
            {
                System.Diagnostics.Debug.WriteLine("ModelState is not Valid");
                System.Diagnostics.Debug.WriteLine(ModelState.Values.SelectMany(v=>v.Errors));
                return Page();
            }
            if (id == null)
            {
               await _context.Continents.AddAsync(Continent);
            }
            else
            {
                _context.Attach(Continent).State = EntityState.Modified;
            }

            await _context.SaveChangesAsync();

            return RedirectToPage("./Index");
        }
    }
}

This the razor page for the model above

    @page "{id?}"
    @model World.Pages.EditContinentModel
    @{
        ViewData["Title"] = "Edit Continent";
    }
    <div >
        <div >
        <h2>@ViewData["Title"]</h2>
    </div>
    <form method="post" >
        <div >
            <label asp-for="Continent.ID">ID</label>
            <input  asp-for="Continent.ID" type="text" required/>
           
        </div>
        <div >
            <label asp-for="Continent.Name">Name</label>
            <input  asp-for="Continent.Name" type="text" required/>
            
        </div>
        <div >
            <input type="submit" value="Save" />
        </div>
    </form>
</div>

This below is my model for the DbSet

    namespace World.Models
    {
        public class Continent
        {
            
            public string ID { get; set; }
          
            public string Name { get; set; }
            
    
        }
    }

CodePudding user response:

changed public async Task<IActionResult> OnGetAsync(string id) to

 public async Task<IActionResult> OnGetAsync(string? id)

Same goes in post and it worked.

  • Related