示例#1
0
        public async Task <ActionResult> Index(SearchViewModel model)
        {
            try
            {
                var searchString = model.Filter.Name;
                var heroesList   = await ApiCall.GetHeroesByName(searchString);

                //if there's no hero with that name
                if (heroesList?.response == "error")
                {
                    ModelState.AddModelError("Filter.Name", "There is no superhero with that name!");
                }

                if (!ModelState.IsValid)
                {
                    model = new SearchViewModel();
                    return(View(model));
                }

                model.SearchResult = heroesList.results;

                return(View(model));
            }
            catch (ApiNotFoundException)
            {
                ModelState.AddModelError("Filter.Name", "The service is temporarily unavailable!");

                model = new SearchViewModel();
                return(View(model));
            }
            catch (Exception)
            {
                throw;
            }
        }