c# - System.Web.MVC.ModelState does not have a definition for IsValid -
in mvc 5 application, have try-catch block in deleteconfirmed actionresult works well. don't repeat code, tried place indicated part of following code block function outside controller. added function customerrorlog class, when did, intellisense indicated "system.web.mvc.modelstate not have definition isvalid."
this link indicates error comes trying use modelstate class instead of property. however, article did not indicate should done instead.
below code have followed do. question need differently can save errorlog entry database table?
this code have in controller
catch (exception e) { var _e = new errorlog().fillandsend(e, "delete", "childactionnames"); // following identical each time use catch(exception e) block unitofworkerrorlog uw = new unitofworkerrorlog(); if (modelstate.isvalid) { uw.errorlogrepository.insert(_e); uw.save(); return view("errormanaged"); } } this do: catch block in controller
catch (exception e) { var _e = new errorlog().fillandsend(e, "delete", "childactionnames"); _e.savealert(_e); // pass errorlog instance savealert function } function in errorlog class
public void savealert(errorlog _e) { // errorlog filled when called controller unitofworkerrorlog uw = new unitofworkerrorlog(); if (modelstate.isvalid) { uw.errorlogrepository.insert(_e); uw.save(); } }
modelstate property of controller base class controllers inherit from. can use in there not outside of controller. can either pass model state around parameter, or makes more sense in case keep logic in controller:
if(modelstate.isvalid) _e.savealert(_e); // pass errorlog instance savealert function as additional point note, makes no sense pass _e savealert method. instead can reference using this inside method.
Comments
Post a Comment