Remote validation parameters?

mc 6,476 Reputation points
2023-06-01T08:08:03.2466667+00:00

I can use remote validation

[Remote("NameValidation","Validation",AdditionalFields="__RequestVerificationToken",ErrorMessage="exists",HttpMethod="POST")]
[Required]
public string Name{get;set;}

it only has one parameter name

how to add additional parameter Id?

I want to validate the Name except current name?

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

1 answer

Sort by: Most helpful
  1. Anonymous
    2023-06-02T02:06:06.3633333+00:00

    Hi @mc

    it only has one parameter name how to add additional parameter Id?

    To add additional parameter with Remote validation, you can use the AdditionalFields attribute. To validate two or more additional fields, provide them as a comma-delimited list, code like this:

        public class MyClass
        {
            [Remote("NameValidation", "Home", 
                AdditionalFields = "__RequestVerificationToken," + nameof(Id), 
                ErrorMessage = "exists", HttpMethod = "POST")]
            [Required]
            public string Name { get; set; }
    
            [Required]
            public string Id { get; set; }
        }
    

    The Home controller:

            public IActionResult RemoteValidationTest()
            {
                return View();
            }
            [HttpPost]
            public IActionResult RemoteValidationTest(MyClass myClass)
            {
                if (ModelState.IsValid)
                {
    
                }
                return View();
            }
             
            [AcceptVerbs("GET", "POST")]
            public IActionResult NameValidation(string Name, string Id)
            {
                if (string.IsNullOrEmpty(Id))
                {
                    return Json($"Id is null.");
    
                }
                else if (Name=="AA" && Id == "A1")
                {
                    return Json($"A user named {Name} already exists.");
                }
    
                return Json(true);
            }
    

    After that the result as below:

    image2

    More detail information, see Model validation in ASP.NET Core MVC and Razor Pages(#Additional fields).


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    0 comments No comments

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.