ASP.NET Core Razor Pages - ModelState.Remove() specific entry of an array item or list item

Joyce 100 Reputation points
2024-08-11T08:13:21.4966667+00:00

Hello,

How do I remove a specific ModelState entry of an array item or list item?

In this sample, ModelState.Remove() fails to remove an array item, apparently a name issue.

@page
@model IndexModel
@{
	ViewData["Title"] = "Home page";
}
<form method="post">
	<input asp-for="Entries[0]" />
	<br />
	<br />
	<input asp-for="Entries[1]" />
	<br />
	<br />
	<input asp-for="Entry2" />
	<br />
	<br />
	<input asp-for="Entry3" />
	<br />
	<br />
	<input type="submit" />
</form>
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace WebApplication1.Pages
{
	[BindProperties]
	public class IndexModel : PageModel
	{
		public string?[] Entries { get; set; } = new string[2];
		public string? Entry2 { get; set; }
		public string? Entry3 { get; set; }
		public void OnGet()
		{
			Entries[0] = "Entries[0]";
			Entries[1] = "Entries[1]";
			Entry2 = "Entry2";
			Entry3 = "Entry3";
		}
		public void OnPost()
		{
			// CS8081:Expression does not have a name.
			// Entries[0] = ModelState.Remove(nameof(Entries[0])) ? 
			//	   "Entries[0] removed" : "Entries[0] NOT removed";

			// returning false: Entries[0] not being removed
			Entries[0] = ModelState.Remove(Entries[0]!.GetType().Name) ? 
				"Entries[0] removed" : "Entries[0] NOT removed";

			// returning true
			Entry2 = ModelState.Remove(nameof(Entry2)) ? 
				"Entry2 removed" : "Entry2 NOT removed";

			// removing all including Entries[0], so as to show above results
			ModelState.Clear();
		}
	}
}

Thanks in advance for any advice or direction.

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

Answer accepted by question author
  1. AgaveJoe 30,411 Reputation points
    2024-08-11T16:37:21.77+00:00

    The keys in your example are Entry2, Entry3, Entries[0], and Entries[1]. The Keys property in the following code is a comma separated list of the ModelState keys with the "Keys" key removed and Entries[0].

    [BindProperties]
    public class IndexModel : PageModel
    {
    	public string?[] Entries { get; set; } = new string[2];
    	public string? Entry2 { get; set; }
    	public string? Entry3 { get; set; }
    	public string Keys { get; set; }
    	public void OnGet()
    	{
    		Entries[0] = "Entries[0]";
    		Entries[1] = "Entries[1]";
    		Entry2 = "Entry2";
    		Entry3 = "Entry3";
    	}
    	public void OnPost()
    	{
    		var result = ModelState.Remove(nameof(Keys));
    		Keys = string.Join(",", ModelState.Keys);
    
    	}
    }
    
    

    Markup

    <form method="post">
    	<input asp-for="Entries[0]" />
    	<br />
    	<br />
    	<input asp-for="Entries[1]" />
    	<br />
    	<br />
    	<input asp-for="Entry2" />
    	<br />
    	<br />
    	<input asp-for="Entry3" />
    	<br />
    	<br />
    	<input type="submit" />
    </form>
    <div>
    	@Model.Keys
    </div>
    
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.