public async Task <IActionResult> SelectCountry(string methodName, string controllerName) { int countryId = 0; if (this.HttpContext.Session.Keys.Contains("CountryId")) { countryId = JsonConvert.DeserializeObject <int>(this.HttpContext.Session.GetString("CountryId")); } else if (this.User.Identity.IsAuthenticated) { var user = await this.userManager.GetUserAsync(this.User); countryId = user.CountryId; } this.ViewBag.CallerMethod = methodName; this.ViewBag.CallerController = controllerName; var viewModel = new SelectCountryViewModel { CountryId = countryId, CountriesItems = await this.countriesService.GetAllAsKeyValuePairsAsync(), }; return(this.View(viewModel)); }
public async Task <IActionResult> Index(SelectCountryViewModel inputModel) { if (!this.ModelState.IsValid) { var viewModel = new SelectCountryViewModel { CountriesItems = await this.countriesService.GetAllAsKeyValuePairsAsync(), }; return(this.View(viewModel)); } this.HttpContext.Session.SetString("CountryId", JsonConvert.SerializeObject(inputModel.CountryId)); var countryId = inputModel.CountryId; return(this.RedirectToAction(nameof(this.IndexCars), new { countryId })); }
public async Task <IActionResult> Index() { if (this.HttpContext.Session.Keys.Contains("CountryId")) { var countryId = JsonConvert.DeserializeObject <int>(this.HttpContext.Session.GetString("CountryId")); return(this.RedirectToAction(nameof(this.IndexCars), new { countryId })); } var viewModel = new SelectCountryViewModel { CountriesItems = await this.countriesService.GetAllAsKeyValuePairsAsync(), }; if (this.User.Identity.IsAuthenticated) { var user = await this.userManager.GetUserAsync(this.User); viewModel.CountryId = user.CountryId; } return(this.View(viewModel)); }