/// <summary> /// 编辑连接表单 /// </summary> /// <param name="id"></param> /// <returns></returns> public ActionResult LinkEdit(int?id, string link_type) { return(RunActionWhenLogin((loginuser) => { var model = new LinkEditViewModel(); id = id ?? 0; if (id > 0) { var link = _ILinkService.GetLinkByID(id.Value); if (link != null) { link_type = link.LinkType; } model.Link = link; } if (!ValidateHelper.IsLenInRange(link_type, 1, 30)) { return Content("缺少连接类型"); } model.LinkType = link_type; ViewData["model"] = model; return View(); })); }
public LinkEditViewModel GetLinkById(int id) { using (var ctx = new DbModel()) { var linkDb = ctx.Links.Single(l => l.ID == id); var linkEditVm = new LinkEditViewModel { Id = linkDb.ID, Title = linkDb.Title, Description = linkDb.Description, Url = linkDb.Url }; var keywordsDb = from k in ctx.Keywords join lk in ctx.LinksKeywords on k.ID equals lk.KeywordID where lk.LinkID == id select k.ID; linkEditVm.Keywords = GetTagsList(); linkEditVm.SelectedKeywords = new List <string>(); foreach (var keyword in keywordsDb) { linkEditVm.SelectedKeywords.Add(keyword.ToString()); } return(linkEditVm); } }
public ActionResult EditLink(int id) { var vm = new LinkEditViewModel(); vm = repo.GetLinkById(id); return(View(vm)); }
public ActionResult AddNew() { var vm = new LinkEditViewModel(); vm.Keywords = repo.GetTagsList(); return(View(vm)); }
public void AddLink(LinkEditViewModel linkVm) { var linkDb = new Links(); linkDb.Title = linkVm.Title; linkDb.Description = linkVm.Description; linkDb.Url = linkVm.Url; int linkId = -1; using (var ctx = new DbModel()) { ctx.Links.Add(linkDb); ctx.SaveChanges(); linkId = linkDb.ID; var linksKeywordsDb = new List <LinksKeywords>(); var keywordsDb = new List <Keywords>(); if (linkVm.SelectedKeywords != null && linkId != -1) { linkVm.SelectedKeywords.ForEach(k => { var intId = Convert.ToInt32(k); var keywordDb = new Keywords(); keywordDb.Keyword = ctx.Keywords.Single(kwd => kwd.ID == intId).Keyword; //keywordDb.Keyword = k.Text; keywordsDb.Add(keywordDb); var linkKeywordDb = new LinksKeywords(); linkKeywordDb.KeywordID = Convert.ToInt32(k); linkKeywordDb.LinkID = linkId; linksKeywordsDb.Add(linkKeywordDb); }); ctx.Keywords.AddRange(keywordsDb); ctx.LinksKeywords.AddRange(linksKeywordsDb); ctx.SaveChanges(); } } }
public void SaveLink(LinkEditViewModel linkVm) { using (var ctx = new DbModel()) { var linkDb = ctx.Links.FirstOrDefault(l => l.ID == linkVm.Id); if (linkDb != null) { linkDb.Title = linkVm.Title; linkDb.Description = linkVm.Description; linkDb.Url = linkVm.Url; } var linkKeywords = ctx.LinksKeywords.Where(lk => lk.LinkID == linkVm.Id).ToList(); foreach (var sk in linkVm.SelectedKeywords) { var skInt = Convert.ToInt32(sk); var existingKeyword = linkKeywords.FirstOrDefault(lk => lk.KeywordID == skInt); if (existingKeyword == null) { ctx.LinksKeywords.Add(new LinksKeywords { LinkID = linkVm.Id, KeywordID = skInt }); } } foreach (var linkKeyword in linkKeywords) { var selectedKeyword = linkVm.SelectedKeywords.FirstOrDefault(sk => sk == linkKeyword.KeywordID.ToString()); if (selectedKeyword == null) { ctx.LinksKeywords.Remove(linkKeyword); } } ctx.SaveChanges(); } }
public ActionResult EditLink(LinkEditViewModel viewModel) { if (!_orchardServices.Authorizer.Authorize(Permissions.ManageDirectoryNav, T("Cannot edit link"))) { return(new HttpUnauthorizedResult()); } try { _directoryNavService.EditLink(new LinkRecord { Id = viewModel.LinkId, Name = viewModel.LinkName, Url = viewModel.Url }); _orchardServices.Notifier.Information(T("Link successfully modified")); return(RedirectToAction("Links", new { titleId = viewModel.TitleId, titleName = viewModel.TitleName })); } catch (Exception exception) { _orchardServices.Notifier.Error(T("Saving link failed: {0}", exception.Message)); } return(RedirectToAction("Links", new { titleId = viewModel.TitleId, TitleName = viewModel.TitleName })); }
// GET: link/Edit/{id} public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var link = await _context.Link.FindAsync(id); if (link == null) { return(NotFound()); } var currentUser = await _userManager.GetUserAsync(User); LinkEditViewModel linkEditViewModel = new LinkEditViewModel() { Link = link }; return(View(linkEditViewModel)); }
public ActionResult EditLink(LinkEditViewModel vm) { repo.SaveLink(vm); return(RedirectToAction("Index")); }
public ActionResult AddNew(LinkEditViewModel vm) { repo.AddLink(vm); return(RedirectToAction("Index")); }