public ActionResult Insert(string username) { AddNewContentModel model = new AddNewContentModel(); using (ZavrsniEFentities db = new ZavrsniEFentities()) { var user = db.User.FirstOrDefault(u => u.Username.Equals(username)); var query1 = (from p in db.Page join c in db.Contributor on p.IDpage equals c.IDpage where c.IDuser == user.IDuser orderby p.name select p).ToList(); model.Page = new SelectList(query1, "IDpage", "name"); var query2 = (from p in db.ContentType orderby p.Description select p).ToList(); model.ContentType = new SelectList(query2, "ID", "Description"); var query3 = (from c in db.City orderby c.CityName select c).ToList(); model.Location = new SelectList(query3, "IDcity", "CityName"); model.Username = username; } return(View(model)); }
public async Task <ActionResult> Insert(string username, AddNewContentModel model) { using (ZavrsniEFentities db = new ZavrsniEFentities()) { var user = db.User.FirstOrDefault(u => u.Username.Equals(username)); var currentUser = User.Identity.GetUserName(); var usernameCurrent = db.User.FirstOrDefault(u => u.Username.Equals(currentUser)); var newContent = db.Content.Create(); if (!Request["ContentTypeDropDown"].Any()) { ModelState.AddModelError("", "Incorrect content."); return(RedirectToAction("Insert", new { Username = username })); } var contSel = Request["ContentTypeDropDown"]; newContent.IDcontentType = Convert.ToInt32(contSel); newContent.Text = model.Text; newContent.IDauthor = user.IDuser; newContent.IsCopied = false; if (model.Title != null) { newContent.Title = model.Title; } else { newContent.Title = "(no title)"; } newContent.IDeditor = user.IDuser; newContent.TimeChanged = DateTime.Now; db.Content.Add(newContent); if (Request["LocationInsert"].Any()) { var location = db.LocationContent.Create(); var locationSelect = Request["LocationInsert"]; location.IDlocation = Convert.ToInt32(locationSelect); location.IDcontent = newContent.IDcontent; location.TimeChanged = DateTime.Now; db.LocationContent.Add(location); db.SaveChanges(); } db.SaveChanges(); if (Request["PageDropDown"].Any()) { var contCopy = db.Content.Create(); contCopy.IDcontentType = newContent.IDcontentType; contCopy.IDauthor = usernameCurrent.IDuser; contCopy.Text = newContent.Text; contCopy.Title = newContent.Title; contCopy.IsCopied = true; db.Content.Add(contCopy); db.SaveChanges(); if (Request["LocationInsert"].Any()) { var locCopy = db.LocationContent.Create(); var locationSelect = Request["LocationInsert"]; locCopy.IDlocation = Convert.ToInt32(locationSelect); locCopy.IDcontent = contCopy.IDcontent; locCopy.TimeChanged = DateTime.Now; db.LocationContent.Add(locCopy); db.SaveChanges(); } var content = db.ContentPage.Create(); var pageSel = Request["PageDropDown"]; content.IDuser = user.IDuser; content.IDpage = Convert.ToInt32(pageSel); content.IDcontent = contCopy.IDcontent; db.ContentPage.Add(content); db.SaveChanges(); } return(RedirectToAction("ViewContent", new { Username = username })); } return(View(model)); }