我有一个应用了验证属性的模型。验证属性是UnitInventoryItemRecipeValidationAttribute。
应用了验证属性的模型是:
[UnitInventoryItemRecipeValidation]
public class UnitInventoryItemBE : InventoryItemBaseBE
{
    public UnitInventoryItemBE()
    {
    }
}
RsiMergeUnitItemAsyncResult 类别:
public class RsiMergeUnitItemAsyncResult : RsiAsyncResult
{
    public UnitInventoryItemBE UnitInventoryItem { get; set; }
}
UnitInventoryItemRecipeValidationAttribute
public class UnitInventoryItemRecipeValidationAttribute : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        UnitInventoryItemBE unitItem = value as UnitInventoryItemBE;
        if (unitItem == null)
        {
            return new ValidationResult("Unit inventory item cannot be null.");
        }
        
        return ValidationResult.Success;
    }
}
此验证属性在TryValidateModel之后手动激发
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize]
public async Task<IActionResult> NewMergeUnitInventoryItem(List<UnitPropertyDifference> differences, Guid? targetId, string returnUrl)
{
    try
    {
        if (ModelState.IsValid)
        {
            RsiMergeUnitItemAsyncResult result = await _inventoryService.NewMergeToUnitInventoryItemAsync(differences, targetId.Value);
            if (result.Succeeded)
            {
                TryValidateModel(result.UnitInventoryItem);
            }
        }
        return View("UnitPropertyDifferenceView", differences);
    }
    catch (Exception ex)
    {
        return RedirectToException(ex);
    }
}
但在某些情况下,value始终为空,尽管传递给TryValidateModel的模型具有该值。我在这里附上了截图。
' 
在下面的屏幕截图中,模型不是空的,但是当调用validation属性时,模型得到的值为空,如下所示。
 
为什么这个TryValidateModel没有得到模型的值,尽管上面有值。在其他一些情况下,模型状态是有效的。