asp.net mvc 2 - MVC 2 Client and Server Validation with Data Annotations - Required not working -


i have create view passed viewmodel. viewmodel contains following:

namespace myproject.viewmodels {     public class myobjectcreateview     {         public myobject myobject { get; set; }         public list<otherobject> otherobjects { get; set; }     } } 

the objects generated using entity framework. have metadata partial class myobject:

[metadatatype(typeof(myobjectmetadata))] public partial class myobject {     // validation rules myobject class      public class myobjectmetadata     {         // validation rules myobjectid         [displayname("myobject")]         [required(errormessage = "please enter myobject id number.")]         [displayformat( applyformatineditmode=true,                                 convertemptystringtonull=true,                                 htmlencode=true)]         [datatype(datatype.text)]         [stringlength(25, errormessage = "must under 25 characters.")]         public object myobjectid { get; set; }           // validation rules title         [required(errormessage = "please enter title myobject.")]         [datatype(datatype.multilinetext)]         [stringlength(200, errormessage = "must under 200 characters.")]         [displayformat(applyformatineditmode = true,                                 convertemptystringtonull = true,                                 htmlencode = true)]         public object title { get; set; } 

etc...

my create view looks this:

<% html.enableclientvalidation(); %>  <% using (html.beginform()) {%>      <%:html.validationsummary(true, "please fix following errors:") %>     <%:html.editorfor(model => model.myobject) %>      <p>         <input type="submit" value="save" />     </p>      <% } %>  <div>     <%:html.actionlink("back list", "index") %> </div> 

finally, editor myobject:

<%@ control language="c#" inherits="system.web.mvc.viewusercontrol<txrp.models.myobject>" %> <%--editortemplate--%>  <fieldset>     <div class="editor-label"><%:html.labelfor(model => model.myobjectid)%></div>     <div class="editor-field">         <%:html.textboxfor(model => model.myobjectid)%>         <%= html.validationmessagefor(model => model.myobjectid) %>     </div>                 <div class="editor-label"><%:html.labelfor(model => model.title)%></div>     <div class="editor-field">         <%:html.textareafor(model => model.title, new { cols = "80" })%>         <%= html.validationmessagefor(model => model.title)%>     </div> 

i have client validation set, , scripts in master page:

<script src="../../scripts/microsoftmvcajax.js" type="text/javascript"></script> <script src="../../scripts/microsoftmvcvalidation.js" type="text/javascript"></script> <script src="../../scripts/microsoftmvcjqueryvalidation.js" type="text/javascript"></script> <script src="../../scripts/jquery-1.4.1.min.js" type="text/javascript"></script>   <script src="../../scripts/ui/minified/jquery.ui.core.min.js" type="text/javascript"></script>   <script src="../../scripts/ui/minified/jquery.ui.datepicker.min.js" type="text/javascript"></script>    <link href="../../content/site.css" type="text/css" rel="stylesheet" />   <link href="../../content/jquery-ui/sunny/jquery-ui-1.8.4.custom.css" type="text/css" rel="stylesheet" />  

when click save button, no validation happens. no client validation, no server validation (it doesn't seem hit post create action @ all!); bombs on entity framework model constraintexception, because title null. argh!

i'm sure it's silly thing i've overlooked, know had working @ 1 point, , it's not, , i've been trying figure out week. help, i'm developing callous on forehead banging on desk!

edit: here controller:

public actionresult create(myobject myobject) {     if (!modelstate.isvalid)      {         //modelstate invalid         return view(new myobject());     }     try     {         //todo: save myobject          return redirecttoaction("index");     }     catch     {         //invalid - redisplay errors          return view(new myobject());     }            } 

and exception stack trace:

at system.data.objects.dataclasses.structuralobject.setvalidvalue(string value, boolean isnullable)    @ myproject.models.myobject.set_title(string value) in c:\codeprojects\myproject\models\myprojectdb.designer.cs:line 4941 

couple of remarks code:

  1. you have view model see looks more entity framework autogenerated model. properties of type object, ... arghhh...
  2. you include both microsoftmvcvalidation.js , microsoftmvcjqueryvalidation.js mutually exclusive. have decide whether client validation using ms technology or jquery validate plugin.

you didn't show code of controller actions nor exact exception stack trace getting. saying post action not hit exception. exception?


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -