//
        // GET: /Account/Create

        public ActionResult Create()
        {
            var sectors = _sectorService.ListIndustrialSectors();
            var viewModel = new AccountViewModel
            {
                Sectors = new SelectList(sectors, "Id", "Sector")
            };

            return View(viewModel);
        }
        public ActionResult Create([Bind(Exclude = "Id")]Account account)
        {
            string userName = this.User.Identity.Name;
            int sectorId = Int32.Parse(Request.Form["Account.SectorId"]);/*we prefix, as the sector is inside the Account editor*/
            /*and isn't directly bound as field of the Account*/

            //we get the owner the account
            User owner = _userService.GetUser(userName);

            //we get the industrial sector from the db
            IndustrialSector sector = _sectorService.GetIndustrialSector(sectorId);

            account.Owner = owner;
            account.IndustrialSector = sector;

            if (!_accountService.CreateAccount(account))
            {
                var sectors = _sectorService.ListIndustrialSectors();
                var viewModel = new AccountViewModel(account)
                {
                    Sectors = new SelectList(sectors, "Id", "Sector")
                };

                return View(viewModel);
            }

            return RedirectToAction("Index");
        }
        public ActionResult Edit(int id, FormCollection formValues)
        {
            // we retrieve existing account from the db
            Account account = _accountService.GetAccount(id);

            if (account == null)
            {
                Response.StatusCode = (int)HttpStatusCode.NotFound;
                return View("NotFound");
            }

            // we update the account with form posted values
            TryUpdateModel(account, "Account" /*prefix, as the account is inside the Account editor*/);
            
            int sectorId = Int32.Parse(Request.Form["Account.SectorId"]);/*we prefix, as the sector is inside the Account editor*/

            //if the account sector has changed, we update it in the model
            if (sectorId != account.IndustrialSector.Id)
            {
                account.IndustrialSector = _sectorService.GetIndustrialSector(sectorId);
            }

            if (!_accountService.EditAccount(account))
            {
                var sectors = _sectorService.ListIndustrialSectors();
                var viewModel = new AccountViewModel(account)
                {
                    Sectors = new SelectList(sectors, "Id", "Sector", account.IndustrialSector.Id)
                };

                return View(viewModel);
            }

            return RedirectToAction("Index");
        }
        //
        // GET: /Account/Edit/5

        public ActionResult Edit(int id)
        {
            //getting the account to edit
            Account account = _accountService.GetAccount(id);

            if (account == null)
            {
                Response.StatusCode = (int)HttpStatusCode.NotFound;
                return View("NotFound");
            }

            var sectors = _sectorService.ListIndustrialSectors();
            var viewModel = new AccountViewModel(account)
            {
                Sectors = new SelectList(sectors, "Id", "Sector", account.IndustrialSector.Id)
            };

            return View(viewModel);

        }