问题
I am working on a MVC Razor project that needs some more complex input validations. We are using ViewModels to remove all access to the data model from the controller without going through a logic layer. What we need to solve is how to do the following types of validations:
User selected date is after another date value:
// Read Only for user
public DateTime StartDate { get; set; }
// Must be after StartDate
public DateTime OccurredAt { get; set; }
Sum of user inputs for N (variable) fields do not exceed the value of another field.
// Read only for user
public double StartingAmount { get; set; }
// Sum of these fields must be less than starting amount
public double AmountTransfered { get; set; }
public double AmountLosses { get; set; }
public double AmountSampled { get; set; }
// Validation Check
if (StartingAmount - (AmountTransfered + AmountLosses + AmountSampled) > 0)
isValid = true;
I am new to MVC, and most validation things I find on Google are from 2010 and are loaded with JavaScript to do custom implementations.
I am hoping that there are newer mechanisms to perform compound validation using attributes to define what fields are related. I suspect that the solution for both of these will be very similar, just a different set of parameters and data types.
来源:https://stackoverflow.com/questions/29868678/mvc5-how-to-validate-compound-checks