public void Save(AddCompanyViewModel model) { if (ModelState.IsValid) { if (NoErrors(model)) { #region Saving data to database var company = new Company { ID = model.ID, Name = model.Name, ActivityID = ++model.ActivityID //SQL index starts from 1, asp dropdownlist index starts from 0 }; int companyId; CompanyRepository.SaveCompany(company); companyId = CompanyRepository.GetLastInsertedIdentity(); foreach (var town in this.ViewStateTowns) { ContactsRepository.AddCompanyTownLink(companyId, town.ID); } #endregion this.ClearErrors(); this.ClearViewState(); this.JavaScriptAlert("Компания сохранена успешно"); } } }