diff --git a/Controllers/newsController.cs b/Controllers/newsController.cs index d3314cd..2d748ff 100644 --- a/Controllers/newsController.cs +++ b/Controllers/newsController.cs @@ -64,6 +64,9 @@ namespace WebApplication1.Controllers // GET: news/Details/5 public ActionResult Details(int? id) { + string userrole = Convert.ToString(Session["Userrole"]); + ViewBag.role = userrole.Contains("tag&catManagment") ? 1 : 0; + if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); @@ -112,7 +115,7 @@ namespace WebApplication1.Controllers if (userrole.Contains("addNews")) { - ViewBag.categoryTitle = db.categories.ToList(); + setLists(); return View(); } else @@ -176,7 +179,9 @@ namespace WebApplication1.Controllers if (!new[] { ".jpg", ".jpeg", ".png" }.Contains(fileExtension)) { ModelState.AddModelError("image", "Only JPG, JPEG, and PNG files are allowed."); + news.image = ""; return View(); + } else { @@ -224,8 +229,8 @@ namespace WebApplication1.Controllers if (Session["User"] != null) { string userrole = Convert.ToString(Session["Userrole"]); - - + setLists(); + if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); @@ -253,11 +258,82 @@ namespace WebApplication1.Controllers // more details see https://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] - public ActionResult Edit([Bind(Include = "ID,title,image,link,summary,cat,tag,publishDate,userID,Content,views")] news news) + public ActionResult Edit([Bind(Include = "ID,title,image,link,summary,cat,tag,publishDate,userID,Content,views")] news news, List Tags, List Cat) { if (ModelState.IsValid) { - db.Entry(news).State = EntityState.Modified; + + news.cat = ""; + if (Tags != null) + { + news.tag = ""; + foreach (string T in Tags) + { + if (T != Tags[0]) + { + news.tag += " ,"; + news.tag += T; + } + else + news.tag += T; + } + } + if (Cat != null){ + foreach (string c in Cat) + { + if (c != Cat[0]) + { + news.cat += " ,"; + news.cat += c; + } + else + news.cat += c; + } + } + var image = Request.Files[0]; + if (image.ContentLength > 1024 * 1024) + { + //check if fields are empty + ModelState.AddModelError("imageFile", "File size cannot exceed 1MB."); + return RedirectToAction("Index"); + } + + // Check file extension + string fileExtension = Path.GetExtension(image.FileName).ToLower(); + //if (fileExtension != ".jpg" || fileExtension != ".jpeg" || fileExtension != ".png") + if (!new[] { ".jpg", ".jpeg", ".png" }.Contains(fileExtension)) + { + ModelState.AddModelError("image", "Only JPG, JPEG, and PNG files are allowed."); + news.image = ""; + return View(); + + } + else + { + int length = 10; // Desired length of the random string + string allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + char[] chars = new char[length]; + Random rand = new Random(); + string imgname = ""; + for (int i = 0; i < length; i++) + { + chars[i] = allowedChars[rand.Next(0, allowedChars.Length)]; + imgname += "" + chars[i]; + } + var path = $"~/Media/news/" + imgname + fileExtension; + image.SaveAs(Server.MapPath(path)); + news.image = path; + } + + //db op + var curnews = db.news.Find(news.ID); + curnews.title = news.title != null ? news.title : curnews.title; + curnews.image = news.image != null ? news.image : curnews.image; + curnews.summary = news.summary != null ? news.summary : curnews.summary; + curnews.Content = news.Content != null ? news.Content : curnews.Content; + curnews.link = news.link != null ? news.link : curnews.link; + curnews.tag = news.tag != null ? news.tag : curnews.tag; + curnews.cat = news.cat != null ? news.cat : curnews.cat; db.SaveChanges(); return RedirectToAction("Index"); } diff --git a/Media/news/kgeN7kkWJ0.jpg b/Media/news/kgeN7kkWJ0.jpg new file mode 100644 index 0000000..1d98158 Binary files /dev/null and b/Media/news/kgeN7kkWJ0.jpg differ diff --git a/Views/news/Details.cshtml b/Views/news/Details.cshtml index c5df213..fa77fd3 100644 --- a/Views/news/Details.cshtml +++ b/Views/news/Details.cshtml @@ -94,6 +94,12 @@

- @Html.ActionLink("Edit", "Edit", new { id = Model.ID }) | + @if (@ViewBag.role == 1) + { + @Html.ActionLink("Edit", "Edit", new { id = Model.ID }) + + | + + } @Html.ActionLink("Back to List", "Index")

diff --git a/Views/news/Edit.cshtml b/Views/news/Edit.cshtml index 6471203..362d6b4 100644 --- a/Views/news/Edit.cshtml +++ b/Views/news/Edit.cshtml @@ -6,15 +6,14 @@

Edit

-@using (Html.BeginForm()) +@using (Html.BeginForm("Edit", "news", FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.AntiForgeryToken() - -
+ +

news


@Html.ValidationSummary(true, "", new { @class = "text-danger" }) - @Html.HiddenFor(model => model.ID)
@Html.LabelFor(model => model.title, htmlAttributes: new { @class = "control-label col-md-2" }) @@ -24,14 +23,6 @@
-
- @Html.LabelFor(model => model.image, htmlAttributes: new { @class = "control-label col-md-2" }) -
- @Html.EditorFor(model => model.image, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.image, "", new { @class = "text-danger" }) -
-
-
@Html.LabelFor(model => model.link, htmlAttributes: new { @class = "control-label col-md-2" })
@@ -41,17 +32,21 @@
- @Html.LabelFor(model => model.summary, htmlAttributes: new { @class = "control-label col-md-2" }) + @Html.LabelFor(model => model.image, htmlAttributes: new { @class = "control-label col-md-2" })
- @Html.EditorFor(model => model.summary, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.summary, "", new { @class = "text-danger" }) + @Html.EditorFor(model => model.image, new { htmlAttributes = new { @type = "file", @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.image, "", new { @class = "text-danger" })
-
- @Html.LabelFor(model => model.cat, htmlAttributes: new { @class = "control-label col-md-2" }) -
- @Html.EditorFor(model => model.cat, new { htmlAttributes = new { @class = "form-control" } }) + +
+ @Html.ValidationMessageFor(model => model.cat, "", new { @class = "text-danger" })
@@ -59,46 +54,35 @@
@Html.LabelFor(model => model.tag, htmlAttributes: new { @class = "control-label col-md-2" })
- @Html.EditorFor(model => model.tag, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.tag, "", new { @class = "text-danger" })
- @Html.LabelFor(model => model.publishDate, htmlAttributes: new { @class = "control-label col-md-2" }) + @Html.LabelFor(model => model.summary, htmlAttributes: new { @class = "control-label col-md-2" })
- @Html.EditorFor(model => model.publishDate, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.publishDate, "", new { @class = "text-danger" }) + @Html.TextAreaFor(model => model.summary, new { @class = "form-control" }) + @Html.ValidationMessageFor(model => model.summary, "", new { @class = "text-danger" })
- -
- @Html.LabelFor(model => model.userID, htmlAttributes: new { @class = "control-label col-md-2" }) -
- @Html.EditorFor(model => model.userID, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.userID, "", new { @class = "text-danger" }) -
-
-
@Html.LabelFor(model => model.Content, htmlAttributes: new { @class = "control-label col-md-2" })
- @Html.EditorFor(model => model.Content, new { htmlAttributes = new { @class = "form-control" } }) + @Html.TextAreaFor(model => model.Content, new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.Content, "", new { @class = "text-danger" })
- -
- @Html.LabelFor(model => model.views, htmlAttributes: new { @class = "control-label col-md-2" }) -
- @Html.EditorFor(model => model.views, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.views, "", new { @class = "text-danger" }) -
-
+
- +
diff --git a/Views/users/allusers.cshtml b/Views/users/allusers.cshtml index 85c00b6..a2140cc 100644 --- a/Views/users/allusers.cshtml +++ b/Views/users/allusers.cshtml @@ -30,7 +30,6 @@ @Html.ActionLink("Edit", "Edituser", new { id=item.ID }) | - @Html.ActionLink("Details", "Details", new { id=item.ID }) | @Html.ActionLink("Delete", "Delete", new { id=item.ID })