Hi @mc
it only has one parameter
namehow to add additional parameterId?
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:
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