I'm working on an ASP.Net core 3.1 MVC project in which I want to create a custom Validator, where shipped qty cannot exceed the ordered qty. I am getting a compile error in the model ([ValidShippedQty(Pending)]) in the ViewModel Error CS0120 An object reference is required for the non-static field, method, or property 'ShipmentsViewModel.Pending'
This is my ViewModel
public class ShipmentsViewModel
{
public string CustomerName { get; set; }
public int OrderID { get; set; }
public int LineItemID { get; set; }
public int Qty { get; set; }
public int ShippedQty { get; set; }
[System.ComponentModel.DefaultValue(0)]
public int Pending { get; set; }
[Required]
[Display(Name = "Ship Date")]
public DateTime thisShipDate { get; set; }
[Required]
[Display(Name = "Courier")]
public string thisCourier { get; set; }
[Required]
[Display(Name = "Qty Shipped")]
[ValidShippedQty(Pending)]
public int thisQty { get; set; }
public List<ShipmentDetails> shipmentDetails;
}
This is my custom validation attribute
public class ValidShippedQty: System.ComponentModel.DataAnnotations.ValidationAttribute
{
public int MaxValue { get; set; }
public ValidShippedQty(int MaximumQtyAllowed)
{
MaxValue = MaximumQtyAllowed;
}
public override bool IsValid(object value)
{
int intvalue = int.Parse(value.ToString());
bool retvalue;
if (intvalue > MaxValue)
retvalue = false;
else
retvalue = true;
return retvalue;
}
}
and the cstml page (part) is as follows
<div class="col-4">
<div class="form-group">
<label asp-for="@Model.thisQty" class="control-label"></label>
<input asp-for="@Model.thisQty" class="form-control" />
<span asp-validation-for="@Model.thisQty" class="text-danger"></span>
<input type="hidden" asp-for="@Model.LineItemID" />
</div>
</div>