MVC5 - How to validate compound checks?

你离开我真会死。 提交于 2020-01-25 21:01:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!