validation - Validate an Object in ASP.NET MVC without passing it into an Action -
in asp.net mvc can validate model passed action modelstate.isvalid()
.
i'd validate arbitrary objects rather 1 model passed in. how can that, using framework's libraries?
public actionresult isvalidsofar() { // user's autosaved data var json = await ... homemodel model = jsonconvert.deserialize<homemodel>(json); // validate model <---- how? } public class homemodel { [required, maxlength(100)] public string name { get; set; } }
you can use validationcontext class ... below
var context = new validationcontext(modelobject); var results = new list<validationresult>(); var isvalid = validator.tryvalidateobject(modelobject, context, results); if (!isvalid) { foreach (var validationresult in results) { //validation errors } }
Comments
Post a Comment