ASP.Net MVC3 conditional validation -


i'm having troubles validation on application. let's i've following models:

    public class company     {         public int id { get; set; }         [required]         public string name { get; set; }         public string location { get; set; }         public list<contacts> contacts { get; set; }     }      public class contact     {         public int id { get; set; }         [required]         public string name { get; set; }         [datatype(datatype.emailaddress)]         public string email { get; set; }         public string telephone { get; set; }         public string mobile { get; set; }     } 

now in company create view i've 2 buttons, 1 add contacts company, , 1 create new company. detected button used in controller (both buttons named "button"):

    [httppost]     public actionresult create(string button, formcollection collection)     {         if(button == "addcontact")         {             addcontact(collection);         }         else         {             createcompany(collection);         }     } 

while it's being created object represents company it's being create stored in session (for example httpcontext.session["company"] = company;)

now problem if, example, try add contact without first specifying company name, validation error because company name required, shouldn't happen because user might want add contacts before adding company info. or if try save company, validation error, because when saving "add contact" form empty, means contact name (which required well) not specified.

what want know if it's possible validate contact properties when addcontact button used, , validate company properties when createcompany button pressed.

for need serve-side, if has solution client-side appreciate help.

you trigger own validation on individual objects using validator.tryvalidateobject(object, validationcontext, icollection)


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -