Click here to Skip to main content
15,904,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

i have a table that has 12 fields, all allow null except the Id field(Primary Key), i am using database first workflow of mvc 5, in my model annotation all 12 fields are required, i am displaying only four fields in one views and the rest in another view, but while trying to insert values into the table from the view that has four fields, it reports error that there are other fields that are required, how do i insert only these four fields, with removing required annotation from the rest? Here is my code:

C#
public ActionResult Estate(Customer cus)
        {
            try
            {
                var customer = new Customer
                {
                    ClientId = cus.ClientId,
                    ClientGroup = cus.ClientGroup,
                    OrgName = cus.OrgName,
                    Branch = cus.Branch
                    
                };
                
                db.Customers.Add(customer);
                db.SaveChanges();

                
            }
            catch (Exception ex)
            {
                
               ModelState.AddModelError("",ex.Message);
            }
            
            return View();
        }
Posted
Comments
Afzaal Ahmad Zeeshan 6-Nov-15 8:33am    
Try to set them to their default values, like default(int) and so on.
Uwakpeter 6-Nov-15 8:38am    
the user wants to be seeing normal database table null while viewing the record.
Krunal Rohit 6-Nov-15 9:50am    
Split up your models for each view.

-KR

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900