How to display alert message box in asp.net core mvc controller? -
i want display alert message box when account model invalid. how should gonna in controller in asp.net core mvc? here controller's code
[httppost] public iactionresult index(account model) { if (model.id != null && model.password != null) { using (var db = new accountdbcontext()) { var account = db.account.firstordefault( => a.id.equals(model.id) && a.password.equals(model.password) ); if(account != null) return redirecttoaction("index", "main"); } //i want display error msg box...here "check id or pw". modelstate.addmodelerror("", "error"); } return view(); } i searched way there no "page" identifier or "response.write()" method in controller class.... how should gonna do?
p.s way checked model's validation right way? (i used model , check it's partial properties, instead using view model )
define error in controller:
modelstate.addmodelerror("error", "check id"); and display in view:
@if (!viewdata.modelstate.isvalid && viewdata.modelstate["error"].errors.count > 0) { <div class="alert alert- <strong>error!</strong> danger">@viewdata.modelstate["error"].errors.first().errormessage </div> } edit
to show alert box, add javascript in view, following uses jquery:
<head> <script type="text/javascript"> @if (!viewdata.modelstate.isvalid && viewdata.modelstate["error"].errors.count > 0) { <text> $(document).ready(function() { alert('@viewdata.modelstate["error"].errors.first().errormessage'); }); </text> } </script> </head>
Comments
Post a Comment