private void InsertTag(ArticleTagsViewModel vm) { string[] tags; tags = vm.tags.Name.Split(','); foreach (var item in tags) { if (getTagByName(item) == null) { Tags temp = new Tags(); temp.Name = item.Trim(); db.Tags.Add(temp); db.SaveChanges(); vm.article.Tags.Add(temp); } else { Tags temp = getTagByName(item); vm.article.Tags.Add(temp); } } }
public ActionResult Create(ArticleTagsViewModel vm) { #region publicip //Visitor IP address //string stringIpAddress; //stringIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; //if (stringIpAddress == null) //{ // stringIpAddress = Request.ServerVariables["REMOTE_ADDR"]; //} #endregion #region lanip //LAN IP address //Get the Host Name string stringHostName = Dns.GetHostName(); //Get The Ip Host Entry IPHostEntry ipHostEntries = Dns.GetHostEntry(stringHostName); //Get The Ip Address From The Ip Host Entry Address List IPAddress[] arrIpAddress = ipHostEntries.AddressList; #endregion if (ModelState.IsValid) { vm.article.DateCreated = DateTime.Now; vm.article.UserIPAddress = arrIpAddress[arrIpAddress.Length - 1].ToString(); db.Articles.Add(vm.article); InsertTag(vm); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.ArticleCategoryID = new SelectList(db.ArticleCategories, "ArticleCategoryID", "Name", vm.article.ArticleCategoryID); return View(vm.article); }