asp.net mvc - ModelState.IsValid is false - But Which One - Easy Way -
in asp.net mvc, when call post action data, check modelstate , in case validation error, falst. big enter user information form, annoying expand each value , @ count see key (9 in attached example image) has validation error. wondering if knows easy way figure out element causing validation error.
i propose write method:
namespace system.web { using mvc; public static class modelstateextensions { public static tuple<string, string> getfirsterror(this modelstatedictionary modelstate) { if (modelstate.isvalid) { return null; } foreach (var key in modelstate.keys) { if (modelstate[key].errors.count != 0) { return new tuple<string, string>(key, modelstate[key].errors[0].errormessage); } } return null; } } }
then during debugging open immediate window , enter:
modelstate.getfirsterror()
Comments
Post a Comment