0

I'm building an ASP.NET MVC5 application with C# and SQL server/EF6.

I have a table with 50 columns. In the first step of the app, I need to post values of 12 column only to this table. Rest of columns will either stay null, or get filled (through update query in EF6), along the app flow and according to user choices.

How to implement a post request in EF6 without throwing an exception of System.ArgumentNullException: Value cannot be null?

    public S_REQUEST Save_S_Without_All_Data(SViewModel vm)
    {
        var s_request = new S_REQUEST
        {
            //mapping model properties with viewmodel properties
        };

        return s_request;
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult PostAction(SViewModel vm)
    {

                switch (selectedStep){
                //other cases
                case "2": 
                try {
                    if (ModelState.IsValid)
                    {
                        var s_request = Save_S_Without_All_Data(vm);
                        db.S_REQUEST.Add(s_request);

                        //add DELEGATED_EMPLOYEE_ID
                        var s_request_extra_data = new S_REQUEST
                        {
                            DELEGATED_EMPLOYEE_ID = vm.DelegatedTeamMemberId
                        };
                        db.S_REQUEST.Add(s_request_extra_data);

                        db.SaveChanges();


                        return View("Success");
                    }
                    return View(vm);
                }
                catch
                {
                    ModelState.AddModelError("SaveError", "Unable to save changes.");
                }
                break; 
                //other cases
     }
  • 2
    Make the properties nullable? – bolkay May 08 '19 at 20:03
  • they are nullable in model. You mean in viewmodel? – Abayomi Nidal May 08 '19 at 20:20
  • Both. You can't save the entity if it is missing a required property and you can't get past validation if a required field is missing from viewmodel. If the above code is catching an exception you might need to drill into the inner exception - probably an [DbEntityValidationException](https://stackoverflow.com/questions/7795300/validation-failed-for-one-or-more-entities-see-entityvalidationerrors-propert). – Steve Greene May 08 '19 at 21:00
  • thank you very much @SteveGreene catching with DbEntityValidationException helped me a lot :) – Abayomi Nidal May 08 '19 at 21:20

0 Answers0