public static HebbraCoDbfModel.BusinessUnit buildModel(BusinessUnitDetailVM businessUnit, HebbraCoDbfModel.BusinessUnit Bu)
 {
     Bu.businessUnitCode = businessUnit.businessUnitCode;
     Bu.title            = businessUnit.title;
     Bu.description      = businessUnit.description;
     Bu.officeAddress1   = businessUnit.officeAddress1;
     Bu.officeAddresss2  = businessUnit.officeAddresss2;
     Bu.officeAddress3   = businessUnit.officeAddress3;
     Bu.officeContact    = businessUnit.officeContact;
     Bu.officeEmail      = businessUnit.officeEmail;
     Bu.officePhone      = businessUnit.officePhone;
     Bu.officePostCode   = businessUnit.officePostCode;
     Bu.Active           = true;
     return(Bu);
 }
        public static Models.BusinessUnitDetailVM buildViewModel(HebbraCoDbfModel.BusinessUnit thisBu)
        {
            BusinessUnitDetailVM vm = new BusinessUnitDetailVM
            {
                businessUnitCode = thisBu.businessUnitCode.Trim(),
                title            = thisBu.title,
                description      = thisBu.description,
                officeAddress1   = thisBu.officeAddress1,
                officeAddresss2  = thisBu.officeAddresss2,
                officeAddress3   = thisBu.officeAddress3,
                officeContact    = thisBu.officeContact,
                officeEmail      = thisBu.officeEmail,
                officePhone      = thisBu.officePhone,
                officePostCode   = thisBu.officePostCode
            };

            return(vm);
        }
        [ValidateAntiForgeryToken] // Tells ASP.NET MVC that we don't want to be vulnerable to CSRF attacks!
        public ActionResult Create([Bind(Include = "businessUnitCode,title,description,officeAddress1,officeAddresss2,officeAddress3,officePostCode,officeContact,officePhone,officeEmail")] Task1Start.Models.BusinessUnitDetailVM businessUnitVM)
        {
            if (ModelState.IsValid) // If validation checks pass...
            {
                if (db.BusinessUnits.Count(bu => bu.businessUnitCode.Equals(businessUnitVM.businessUnitCode, StringComparison.OrdinalIgnoreCase) && bu.Active == true) > 0)
                {
                    ViewBag.Message = "The business unit code is already in use!";
                    return(View(businessUnitVM));
                }
                else
                {
                    var model = BusinessUnitDetailVM.buildModel(businessUnitVM); // Passes the view model data and gets back a BusinessUnit model
                    model.Active = true;                                         // Sets the active flag to true (it's not been soft deleted!)
                    db.BusinessUnits.Add(model);                                 // Inserts the data to the database as a new row
                    db.SaveChanges();                                            // Saves the changes to the database
                    return(RedirectToAction("Index"));                           // Redirects to the BusinessUnits list
                }
            }

            return(View(businessUnitVM)); // Returns back to the creation form with the errors from validation
        }