Model Validation in ASP.NET MVC -
i not getting validation messages? idea how resolve? please take @ view, model, , controller code below. attached js files maybe im missing files?
@model mvcapplication1.models.assesment <link href="../../content/site.css" rel="stylesheet" type="text/css" /> <script src="../../scripts/jquery.validate.min.js" type="text/javascript"></script> <script src="../../scripts/jquery.validate.min.js" type="text/javascript"></script> @using (html.beginform()) { @html.textboxfor(m => m.name) @html.validationmessagefor(m=>m.name,"*hello") } <input type="submit" value="submit" />
using system; using system.collections.generic; using system.componentmodel; using system.web.mvc; using system.componentmodel.dataannotations; namespace mvcapplication1.models { public class assesment { [required] public string name { get; set; } } } public class registercontroller : controller { [httpget] public actionresult index() { return view(); } [httppost] public actionresult index(assesment assesment) { return view(); } }
your <input type="submit">
should inside form.
also, should pass invalid model view when handling post
[httppost] public actionresult index(assesment assesment) { return view(assesment); }
by way, typical httppost
action looks this:
[httppost] public actionresult index(assesment assesment) { if( modelstate.isvalid ) { // handle post data (write db, etc.) //... // redirect new page return redirecttoaction( ... ); } // show same view again, time validation errors return view(assesment); }
Comments
Post a Comment