public ActionResult DeleteCompanyTag(CompanyTagVM model) { var userCompany = User as MPrincipal; var login = userCompany.UserDetails.Login; using (ApplicationDbContext db = new ApplicationDbContext()) { var company = db.Companies.FirstOrDefault(x => x.Login == login); var tag = db.CompanyTags.FirstOrDefault(t => t.TagId == model.Id && t.CompanyId == company.Id); db.CompanyTags.Remove(tag); db.SaveChanges(); } return(RedirectToAction("CompanyTagsList")); }
public ActionResult CompanyTagsList() { var user = User as MPrincipal; var login = user.UserDetails.Login; ViewBag.UserName = user.UserDetails.Login; ViewBag.UserRole = 4; CompanyTagsVM model = null; using (ApplicationDbContext db = new ApplicationDbContext()) { var company = db.Companies.FirstOrDefault(u => u.Login.Equals(login)); model = new CompanyTagsVM(); var dbCompanyTags = db.Tags.Where(t => t.CompanyTag.Any(c => c.CompanyId == company.Id)).ToList(); ViewBag.NoTags = false; if (dbCompanyTags == null) { ViewBag.NoTags = true; } List <CompanyTagVM> ctVMList = new List <CompanyTagVM>(); for (int i = 0; i < dbCompanyTags.Count(); i++) { CompanyTagVM ctVM = new CompanyTagVM() { Id = dbCompanyTags[i].Id, Name = dbCompanyTags[i].Name, DeleteFromCompany = dbCompanyTags[i].IsDelete }; ctVMList.Add(ctVM); } model.CompanyTags = ctVMList; List <SelectListItem> allTags = new List <SelectListItem>(); allTags = (from t in db.Tags orderby t.Name select new SelectListItem { Text = t.Name, Value = t.Id.ToString() }).ToList(); List <SelectListItem> otherTagsList = new List <SelectListItem>(); foreach (var item in allTags) { otherTagsList.Add(item); if (model.CompanyTags != null) { foreach (var i in model.CompanyTags) { if (item.Value == i.Id.ToString()) { otherTagsList.Remove(item); } } } } model.OtherTags = otherTagsList; } return(View("CompanyTagsList", model)); }