public ActionResult Index(SelectListItemViewModel m) { // Get all the animals and continents that we want pass to the selectlist // and assign them to the corresponding properties var animals = Help.GetAnimals(); var continents = Help.GetContinents(); // Construct the SelectListItem objects var continentList = continents.Select(c => new SelectListItem { Value = c.ContinentID.ToString(), Text = c.Name, Selected = m.SelectedContinentID == c.ContinentID }).ToList(); var animalList = animals.Select(a => new SelectListItem { Value = a.AnimalID.ToString(), Text = a.Name, Selected = m.SelectedAnimalsIDs.Contains(a.AnimalID) }); // Re-populate animal and continent lists: m.ContinentList = continentList; m.AnimalList = animalList; // get the names of selected continent and animals m.ContinentName = continents.Where(c => c.ContinentID == m.SelectedContinentID).FirstOrDefault().Name; m.AnimalNames = new List <string>(); foreach (var a in animals) { if (m.SelectedAnimalsIDs.Contains(a.AnimalID)) { m.AnimalNames.Add(a.Name); } } return(View(m)); }
public async Task <IViewComponentResult> InvokeAsync() { SelectListItemViewModel selectList = new SelectListItemViewModel { SelectListCategory = await _db.Categories.Select(d => new SelectListItem { Text = d.Name.ToString(), Value = d.Id.ToString() }).ToListAsync(), }; return(View(selectList)); }
public async Task <IViewComponentResult> InvokeAsync() { SelectListItemViewModel viewModel = new SelectListItemViewModel { SelectListCurrentWorkers = await dbContext.CurrentWorkPlaces.Select(d => new SelectListItem { Text = d.Worker.Name + " " + d.Worker.Surname + " " + d.Worker.FatherName, Value = d.Worker.Id.ToString() }).ToListAsync(), }; return(View(viewModel)); }
public async Task <IViewComponentResult> InvokeAsync() { SelectListItemViewModel viewModel = new SelectListItemViewModel() { SelectListPositions = await _context.Positions.Select(d => new SelectListItem { Text = d.PositionName, Value = d.Id.ToString() }).ToListAsync(), }; return(View(viewModel)); }
public void WhenItemIsSelected_CheckImageIsChecked() { var sut = new SelectListItemViewModel { Title = "Test", Selected = false }; Assert.AreEqual(SelectListItemViewModel.UncheckedImage, sut.CheckImage); sut.Selected = true; Assert.AreEqual(SelectListItemViewModel.CheckedImage, sut.CheckImage); }
public async Task <IViewComponentResult> InvokeAsync() { SelectListItemViewModel viewModel = new SelectListItemViewModel { SelectListWorkers = await _context.Workers.Select(d => new SelectListItem { Text = d.Employee.Name + " " + d.Employee.Surname, Value = d.Id.ToString() }).ToListAsync(), }; return(View(viewModel)); }
public async Task <IViewComponentResult> InvokeAsync() { SelectListItemViewModel viewModel = new SelectListItemViewModel { SelectListShops = await _dbContext.Shops.Select(d => new SelectListItem { Text = d.Name, Value = d.Id.ToString() }).ToListAsync(), }; return(View(viewModel)); }
public async Task <IViewComponentResult> InvokeAsync() { SelectListItemViewModel selectList = new SelectListItemViewModel { SelectListBrand = await _dbContext.Brands.Select(d => new SelectListItem { Text = d.Name, Value = d.Id.ToString() }).ToListAsync(), }; return(View(selectList)); }
public ActionResult AddOrEdit(int id = 0) { var model = new BookDto(); var responseAth = WebApiClient.GetAsync("Authors").Result; var ahtList = responseAth.Content.ReadAsAsync <IEnumerable <AuthorDto> >().Result; var authorDtos = ahtList as AuthorDto[] ?? ahtList.ToArray(); model.Authors = authorDtos.Select(a => new SelectListItemViewModel { Id = a.Id, Text = a.FirstName + " " + a.LastName }).ToList(); HttpResponseMessage responseCtgr = WebApiClient.GetAsync("Categories").Result; var ctgrList = responseCtgr.Content.ReadAsAsync <IEnumerable <CategoryDto> >().Result; var ctgrDtos = ctgrList as CategoryDto[] ?? ctgrList.ToArray(); model.Categories = ctgrList.Select(c => new SelectCategoryListItemViewModel { Id = c.Id, Title = c.Title }).ToList(); if (id == 0) { return(View(model)); } var response = WebApiClient.GetAsync("Books/" + id).Result; model = response.Content.ReadAsAsync <BookDto>().Result; model.Authors = authorDtos.Select(a => { var viewModel = new SelectListItemViewModel { Id = a.Id, Text = a.FirstName + " " + a.LastName }; return(viewModel); }).ToList(); model.Categories = ctgrDtos.Select(a => { var viewModel = new SelectCategoryListItemViewModel() { Id = a.Id, Title = a.Title }; return(viewModel); }).ToList(); return(View(model)); }
public ActionResult Index() { // Get all the animals and continents that we want pass to the selectlist // and assign them to the corresponding properties var animals = Help.GetAnimals(); var continents = Help.GetContinents(); // Preselected items var selectedAnimalIDs = new int[] { 2, 5, 8 }; var selectedContinentID = 4; // Construct the SelectListItem objects var continentList = continents.Select(c => new SelectListItem { Value = c.ContinentID.ToString(), Text = c.Name, Selected = c.ContinentID == selectedContinentID ? true : false }).ToList(); var animalList = animals.Select(a => new SelectListItem { Value = a.AnimalID.ToString(), Text = a.Name, Selected = selectedAnimalIDs.Contains(a.AnimalID) }); // Construct the view model that we want to pass to the view var viewModel = new SelectListItemViewModel { PageTitle = "Using a collection of SelectListItems as view model property", // The view's Razor helper will receive a collection of SelectListItem object. // The value of properties of each select list item object are set AnimalList = animalList, ContinentList = continentList, OptionLabel = "*** Please select a continent! ***", // It can be null (default value), so no animals will be preselected. // If you assign a value, animals with the ID of those numbers will be preselected SelectedAnimalsIDs = selectedAnimalIDs, // It can be 0 (default value), so no continent will be preselected // If you assign a value, continent with that ID will be preselected SelectedContinentID = 4, // Names of the continent and animals before submission. // If using default value (null), check for null in the view ContinentName = string.Empty, AnimalNames = new List <string>() }; return(View(viewModel)); }