as per https://www.mikesdotnetting.com/article/343/improved-remote-validation-in-razor-pages I followed the tutorial and implemented the PageRemote. However it does not work if applied to a property of a model and I use the model as property.
public class Draft
{
public int Id { get; set; }
[PageRemote(ErrorMessage = "Invalid data", AdditionalFields = "__RequestVerificationToken", HttpMethod = "post", PageHandler = "CheckReference")]
public string Reference { get; set; }
}
[BindProperty]
public Draft Draft { get; set; }
public JsonResult OnPostCheckReference()
{
var valid = !Draft.Reference.Contains("12345");
return new JsonResult(valid);
}
on my page
<tab>
<tab-item icon="fas fa-arrow-left" url="@Url.Page("../Index")"></tab-item>
<tab-item icon="fas fa-list" url="@Url.Page("Index")"></tab-item>
<tab-item icon="fas fa-plus" is-active="true"></tab-item>
</tab>
<form method="post">
<card>
<card-header icon="fas fa-plus" title="Draft"></card-header>
<card-body>
<input asp-for="Draft.Reference" />
<span asp-validation-for="Draft.Reference" class="text-danger"></span>
</card-body>
<card-footer>
<button class="btn btn-success"><i class="fas fa-plus"></i> Adicionar </button>
</card-footer>
</card>
</form>
@section Scripts{
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
<script src="~/lib/jquery-ajax-unobtrusive/dist/jquery.unobtrusive-ajax.min.js"></script>
}