Example #1
0
        public ActionResult Edit(int id, FormCollection formValues)
        {
            // we retrieve existing opportunity from the db
            Opportunity opportunity = _opportunityService.GetOpportunity(id);

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

            // we update the opportunity with form posted values
            TryUpdateModel(opportunity, "Opportunity" /*prefix, as the opportunity is inside the Opportunity editor"*/);

            int accountId = Int32.Parse(Request.Form["Opportunity.AccountId"]);/*we prefix, as the account is inside the Opportunity editor*/

            //if the opportunity account has changed, we update it in the model
            if (accountId != opportunity.Account.Id)
            {
                opportunity.Account = _accountService.GetAccount(accountId);
            }


            if (!_opportunityService.EditOpportunity(opportunity))
            {
                var accounts  = _accountService.ListAccounts();
                var viewModel = new OpportunityViewModel(opportunity)
                {
                    Accounts = new SelectList(accounts, "Id", "Name")
                };

                return(View(viewModel));
            }

            return(RedirectToAction("Index"));
        }