asp.net mvc - How to ignore required field in form in Entity Framework, MVC C# -


i ask if possible ignore required field in form. problem have complex form in mvc using entity framework want create insurance , bind insurance firm or person, not both. when leave example form person empty, shout out personrodnecislo required field.

firm table , person table in 1...n relationship insurance table, coz person can have multiple insurances same firm, cannot opposite side.

for table firm primary key ico table person primary key personrodnecislo table insurance primary key insurancenumber

so in database model insurance table has 2 foreign keys (ico , personrodnecislo)

i tried use isnullorwhitespace method problem wont reach create method have force ef somehow ignore 1 of 2 field if 1 of them filled

code [post] create method

        public actionresult create([bind(include = "personrodnecislo, firstname, lastname, street, city, psc, phonenumber, secondarystreet, secondarycity, secondarypsc")]person person,                                [bind(include = "insurancenumber,premium,paymentfrequency,annualdate,begindate,enddate,producttype,personrodnecislo")] insurance insurance,                                [bind(include = "ico,street,city,psc")] firm firm)     {         try         {             viewbag.producttype = new selectlist(db.product, "producttype", "productname");             // todo: add insert logic here             if (modelstate.isvalid)             {                 if(string.isnullorwhitespace(firm.ico.tostring()) && string.isnullorwhitespace(person.personrodnecislo.tostring()))                 {                     modelstate.addmodelerror("ico", "vložte prosím iČo nebo rodné číslo");                 }                 if (!(string.isnullorwhitespace(firm.ico.tostring())))                 {                     insurance.personrodnecislo = person.personrodnecislo;                                   if (!(db.person.any(x => x.personrodnecislo == person.personrodnecislo)))                     {                         db.person.add(person);                         db.insurance.add(insurance);                     }                     else                     {                         db.insurance.add(insurance);                     }                 }                 if (string.isnullorwhitespace(person.personrodnecislo.tostring()))                 {                     insurance.ico = firm.ico;                                    if (!(db.firm.any(x => x.ico == firm.ico)))                     {                         db.firm.add(firm);                         db.insurance.add(insurance);                     }                     else                     {                         db.insurance.add(insurance);                     }                 }                 db.savechanges();             }             return redirecttoaction("index");         }         catch         {             return view(insurance);         }     } 


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -