c# - How to make a validation of textbox in asp mvc and entity framework -
i developing asp .net mvc 3 application using c# , sql server 2005.
i using entity framework , code first method.
i have textbox want accept numbers (int).
i tried javascript failed because in form of class editor for.
example :
<div><%:html.label("nombre de passage :")%><%: html.editorfor(model=>model.nbr_passage)%></div>
so, found in tuto have use data annotations
i tried in model :
[range(1, 100, errormessage = "price must between 1 , 100")] public int nbr_passage { get; set; }
but when press button submit, error appears :
validation failed 1 or more entities. see 'entityvalidationerrors' property more details
i made research it,,i found have use try catch. don't know , how use it.
this code of controller (function when submit) :
public actionresult save(flowviewmodel model) { if (modelstate.isvalid) { gamme g = new gamme(); g.id_gamme = model.selectedprofile_ga; g.id_poste = model.selectedposte; g.last_posts = model.posteprecedentselected; g.next_posts = model.postesuivantselected; g.nbr_passage = int.parse(model.nbr_passage); g.position = int.parse(model.position); listg.add(g); db.gammes.add(g); db.savechanges(); } return redirecttoaction("index"); }
update : code of javascript affect on button open view :
<input type="button" value="configurer" id="btnshowgestion" /></div> <div id="divgestion"><%: html.partial("gestion", model) %></div> <% } %> <script type="text/javascript"> $(document).ready(function () { $('#btnshowgestion').click(function () { $('#divgestion').slidetoggle("slow") }); }); </script> <fieldset>
first of all, looks flowviewmodel uses string
nbr_passage
, otherwise don't understand int.parse
. int should work fine.
second, don't see html.validationmessagefor
calls add validators fields , don't mention call html.validationsummary
render summary form whole. want add (one of) those.
Comments
Post a Comment