asp.net mvc - Try Catch, ModelState.IsValid, or Both? -
i wondering, best way test validity , errors in basic crud actions?
when first used generate scaffolded mvc controllers, had this:
if (modelstate.isvalid) { // stuff return view("successfulview") } // if got far went wrong, redisplay return view()
but this:
try { // stuff return redirecttoaction("successfulview"); } catch { return view(); }
testing modelstate not same testing exception, i'm tempted put them both in.
but wonder why ms didn't put both in when updated scaffolding code (which no doubt did reason).
plus, every basic action starts become quite convoluted:
if (modelstate.isvalid) { try { // stuff return redirecttoaction("successfulview"); } catch { return view(); } } // if got far went wrong, redisplay return view()
modelstate - validates viewmodel data annotations would've applied.
trycatch - catch exceptions may occur in code.
i both!
this how,
if (modelstate.isvalid) { try { // stuff return view(); } catch(exception ex) // catches exceptions { return view(ex.message); } } modelstate.addmodelerror("", "error"); return view(model);
Comments
Post a Comment