I am having an issue with using Remote Validation when I have more than 1 parameter

40 Views Asked by At

I am having an issue when trying to send more than one parameter with remote validation in my application. For instance, if my model has these three properties ItemNumber, FromLocation and Quantity. I want to pass the ItemNumber and FromLocation when I do the check for Quantity.

public class ItemModel
{
    public string ItemNumber { get; set; } = string.Empty;
    public string FromLocation { get; set; } = string.Empty;
       
    [Required, Range(0.001, 9999999999), 
        DisplayName("Qty"), 
        PageRemote(ErrorMessage = "Invalid Quantity", 
        HttpMethod = "post", 
        PageHandler = "CheckQuantity", 
        AdditionalFields = nameof(ItemNumber))]
    public decimal Quantity { get; set; }   
)

I have seen an example or two online of adding using the additional fields in the Remote Attribute (as I did above) with AdditionalFields = nameof(ItemNumber). However, in my application my additional fields attribute is in the form (shown below).

<td>
    <label asp-for="Item.Quantity" class="control-label"></label>
</td>
<td>
    <input data-val-remote-additionalfields="__RequestVerificationToken" asp-for="Item.Quantity" class="form-control" />
</td>
<td>
    <span asp-validation-for="Item.Quantity" class="text-danger"></span>
</td>

Is it possible for me to add the ItemNumber and Location with the Quantity look up?

For clarification when the Quantity text box is left the Quantity gets passed to the JsonResult below but the rest of the ItemModel is null or empty.

public async Task<JsonResult> OnPostCheckQuantity(ItemModel item)
{
    ItemCheck Quantity = await ItemCheck.CheckQuantityAsync(
        item.ItemNumber, 
        item.FromLocation);

    if (Quantity.Qauntity < item.Quantity)
    {
        return new JsonResult("Quantity is greater than available for that location.");
    }
    else
    {
        return new JsonResult(true);
    }
}
1

There are 1 best solutions below

0
Buck Hicks On BEST ANSWER

I was able to figure this out. The key was to add the properties to the input tag after the RequestVerificationToken (shown below).

<input 
    data-val-remote-additionalfields="__RequestVerificationToken,
    Item.ItemNumber,Item.FromLocation" 
    asp-for="Item.Quantity" class="form-control" />