I have a Web API model of the lines
class SampleModel
{
[ComponentExistsValidation]
public Guid? ComponentID { get; set; }
...
}
I need to validate that the ComponentID exists under a given model, and the modelid is available to my controller as a route parameter.
[Route("api/model/addcomponents/{modelid:int}")]
public async Task AddComponents(int modelid, [FromBody]SampleModel[] components)
Here is my validator
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value == null)
return ValidationResult.Success;
Guid componentid = (Guid)value;
int modelid; // How do I get this here?
Model context_mdl = Model.GetModel(modelid);
if(!context_mdl.HasComponent(componentid))
{
return new ValidationResult(string.Format("Invalid component"));
}
}
Can I access the modelid route parameter in the validator?