c# - MVC4 and ViewModel - Validation Message not displayed -


i'm developing web application using mvc 4 , entity framework. i'm using viewmodel represents product create/edit/delete. have created validation class check entries , displays error message when necessary.

since i'm using viewmodel, unfortunately error messages not displayed anymore? how should proceed?

here's viewmodel :

public class productviewmodel {      public product product { get; set; }      public producttype producttype { get; set; }      public list<selectlistitem> productcompanies { get; set; } } 

here's action (create, example) :

[httppost] public actionresult create(productviewmodel pvm) {     pvm.productcompanies = db.productcompanies.tolist().select(s => new selectlistitem     {         text = s.name,         value = s.id_productcompany.tostring()     }).tolist();      viewbag.id_productcompany = new selectlist(db.productcompanies, "id_productcompany", "name", pvm.producttype.id_productcompany);      if (modelstate.isvalid)     {          modelstatedictionary errors = validator.isvalid(pvm.producttype);          if (errors.count > 0)         {             modelstate.merge(errors);             return view(pvm);         }           product product = new product         {             purchasedate = pvm.product.purchasedate,             serialnumber = pvm.product.serialnumber,             id_producttype = pvm.producttype.id_producttype         };          producttype producttype = new producttype         {             model = pvm.producttype.model,             catalogprice = pvm.producttype.catalogprice,             id_productcompany = pvm.producttype.id_productcompany         };          db.producttypes.addobject(producttype);         db.products.addobject(product);         db.savechanges();         return redirecttoaction("index", "person");         }      return view(pvm); } 

my own validator :

public static modelstatedictionary isvalid(producttype element) {     modelstatedictionary errors = new modelstatedictionary();      if (!regex.ismatch(element.model, @"^[a-za-z0-9\s][a-za-z-_0-9\s]+$"))     {         errors.addmodelerror("model", "invalid name !");     }      return errors; } 

and message should displayed :

<div class="editor-label">     model :  </div> <div class="editor-field">     @html.textboxfor(model => model.producttype.model, new { maxlength = 50 })     @html.validationmessagefor(model => model.producttype.model) </div> 

maybe changing to

maybe `errors.addmodelerror("producttype.model", "invalid name !"); 

would resolve problem.

by way, best solution use dataannotations , create own validationattribute:

public sealed class validnameattribute : validationattribute {     public override bool isvalid(object value)     {         ....check regex here...     } } 

then should decorate name property validname attribute.

edit:

you can implement ivalidatableobject interface in view model. asp.net mvc pick , automatic validation.

public class myviewmodel: ivalidatableobject {        public ienumerable<validationresult> validate(validationcontext validationcontext)     {         if (regexfails)         {             results.add(new validationresult("please enter valid name."));         }     } } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -