asp.net - MVC Validations not firing -


i'm using asp.net mvc 2. form validations not firing. can not figure out whats wrong here.

model:

 public class stock     {         public int stockid { get; set; }          [required(errormessage = "please select client")]         public int clientid { get; set; }           [required(errormessage = "please select item")]         public int itemid { get; set; }              [required(errormessage = "please enter item count")]           public int itemcount { get; set; }             [required(errormessage = "please enter item price")]         public double price { get; set; }             [required(errormessage = "please enter other expences")]         public double otherexpences { get; set; }          public double totalstockvalue { get; set; }            [required(errormessage = "please enter delivery date")]         public datetime deliverydate { get; set; }          public string description { get; set; }         public list<client> lstclient { get; set; }         public ienumerable<item> lstitem { get; set; }          public string clientname { get; set; }         public string itemname { get; set; }     } 

controller:

[httppost]         public actionresult create(formcollection collection)         {                  try                 {                     if (modelstate.isvalid)                     {                         stockrepository rep = new stockrepository();                         stock stock = new stock();                         stock.clientid =convert.toint32(request.form["clientid"]);                         stock.deliverydate =convert.todatetime(request.form["deliverydate"]);                         stock.description = request.form["description"];                         stock.itemcount =convert.toint32(request.form["itemcount"]);                         stock.itemid =convert.toint32(request.form["itemid"]);                         stock.price =convert.todouble(request.form["price"]);                         stock.otherexpences = convert.todouble(request.form["otherexpences"]);                         stock.totalstockvalue =convert.todouble((stock.price * stock.itemcount)+stock.otherexpences);                         rep.create(stock);                         rep.save();                         return redirecttoaction("index");                     }                     else                         return view();                 }                 catch                 {                     return view();                 }          } 

view:

<%@ page title="" language="c#" masterpagefile="~/stockmasterpage.master" inherits="system.web.mvc.viewpage<stockmanagement.models.stock>" %> <asp:content id="content1" contentplaceholderid="titlecontent" runat="server">     create </asp:content> <asp:content id="content2" contentplaceholderid="maincontent" runat="server">   <link rel="stylesheet" href="<%= url.content("http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css") %>" />   <script src="<%= url.content("http://code.jquery.com/jquery-1.9.1.js") %>"></script>   <script src="<%= url.content("http://code.jquery.com/ui/1.10.3/jquery-ui.js") %>"></script>    <script>       $(function () {           $("#deliverydate").datepicker();       });   </script>      <h2>create</h2>      <% using (html.beginform()) {%>         <%: html.validationsummary(true) %>          <fieldset>             <legend>fields</legend>               <div class="editor-label">                 <%: html.label("select client")%>             </div>             <div class="editor-field">                 <%: html.dropdownlistfor(x => x.clientid, new selectlist(model.lstclient, "clientid", "name"), "-- please select client --") %>                  <%: html.validationmessagefor(model => model.clientid)%>             </div>              <div class="editor-label">                 <%: html.label("select item") %>             </div>             <div class="editor-field">                <%: html.dropdownlistfor(x => x.itemid, new selectlist(model.lstitem, "itemid", "itemname"), "-- please select item --")%>                  <%: html.validationmessagefor(model => model.itemid)%>             </div>               <div class="editor-label">                 <%: html.labelfor(model => model.itemcount) %>             </div>             <div class="editor-field">                 <%: html.textboxfor(model => model.itemcount) %>                 <%: html.validationmessagefor(model => model.itemcount) %>             </div>              <div class="editor-label">                 <%: html.labelfor(model => model.price) %>             </div>             <div class="editor-field">                 <%: html.textboxfor(model => model.price) %>                 <%: html.validationmessagefor(model => model.price) %>             </div>              <div class="editor-label">                 <%: html.labelfor(model => model.otherexpences) %>             </div>             <div class="editor-field">                 <%: html.textboxfor(model => model.otherexpences) %>                 <%: html.validationmessagefor(model => model.otherexpences) %>             </div>              <div class="editor-label">                 <%: html.labelfor(model => model.deliverydate) %>             </div>             <div class="editor-field">                 <%: html.textboxfor(model => model.deliverydate) %>                 <%: html.validationmessagefor(model => model.deliverydate) %>             </div>              <div class="editor-label">                 <%: html.labelfor(model => model.description) %>             </div>             <div class="editor-field">                 <%: html.textareafor(model => model.description) %>                 <%: html.validationmessagefor(model => model.description) %>             </div>              <p>                 <input type="submit" value="create" />             </p>         </fieldset>      <% } %>      <div>         <%: html.actionlink("back list", "index") %>     </div>  </asp:content> 

you should have set clientvalidationenabled true in web config or add following code before using beginform

 <% html.clientvalidationenabled = true; %> 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -