From b68d3f3185eaad0a0664d0188a734bd5b3a38ef7 Mon Sep 17 00:00:00 2001 From: lianpi3 Date: Mon, 29 Jul 2024 14:54:26 +0330 Subject: [PATCH] tag pages news create form correction --- Controllers/HomeController.cs | 10 +- Controllers/categoriesController.cs | 3 +- Controllers/newsController.cs | 19 +++- Controllers/tagsController.cs | 141 ++++++++++++++++++++++++ Views/Shared/_Layout.cshtml | 9 +- Views/categories/Create.cshtml | 4 +- Views/categories/Delete.cshtml | 2 +- Views/news/Create.cshtml | 162 +++++++++++++--------------- Views/tags/Create.cshtml | 36 +++++++ Views/tags/Delete.cshtml | 40 +++++++ Views/tags/Details.cshtml | 49 +++++++++ Views/tags/Edit.cshtml | 45 ++++++++ Views/tags/Index.cshtml | 39 +++++++ WebApplication1.csproj | 6 ++ 14 files changed, 467 insertions(+), 98 deletions(-) create mode 100644 Controllers/tagsController.cs create mode 100644 Views/tags/Create.cshtml create mode 100644 Views/tags/Delete.cshtml create mode 100644 Views/tags/Details.cshtml create mode 100644 Views/tags/Edit.cshtml create mode 100644 Views/tags/Index.cshtml diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index ca324f3..9b262ee 100644 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -13,21 +13,27 @@ namespace WebApplication1.Controllers public ActionResult Index() { + setTitle(); return View(); } public ActionResult About() { ViewBag.Message = "Your application description page."; - + setTitle(); return View(); } public ActionResult Contact() { ViewBag.Message = "Your contact page."; - + setTitle(); return View(); } + + void setTitle() + { + ViewBag.titlemenu = "hi user"; + } } } \ No newline at end of file diff --git a/Controllers/categoriesController.cs b/Controllers/categoriesController.cs index adc837d..087918b 100644 --- a/Controllers/categoriesController.cs +++ b/Controllers/categoriesController.cs @@ -33,7 +33,8 @@ namespace WebApplication1.Controllers { return HttpNotFound(); } - return View(db.news.Where(n => n.title.Contains(category.title))); + //search for record that contains "cat title" in cat field. + return View(db.news.Where(n => n.cat.Contains(category.title))); } // GET: categories/Create diff --git a/Controllers/newsController.cs b/Controllers/newsController.cs index 4a76f7c..343a4ae 100644 --- a/Controllers/newsController.cs +++ b/Controllers/newsController.cs @@ -38,7 +38,23 @@ namespace WebApplication1.Controllers // GET: news/Create public ActionResult Create() { - return View(); + if (Session["User"] != null) + { + var news = db.news.ToList(); + var cat = db.categories.ToList(); + ViewBag.categoryTitle = cat; + var tags= db.tags.ToList(); + ViewBag.tagTitle = tags; + //var models= new Tuple, List>(cat, tags); + return View(); + } + if (Session["User"] != null) + { + ViewBag.categoryTitle = db.categories.ToList(); + return View(); + } + else + return RedirectToAction("login", "users"); } // POST: news/Create @@ -50,6 +66,7 @@ namespace WebApplication1.Controllers { if (ModelState.IsValid) { + db.news.Add(news); db.SaveChanges(); return RedirectToAction("Index"); diff --git a/Controllers/tagsController.cs b/Controllers/tagsController.cs new file mode 100644 index 0000000..8842eca --- /dev/null +++ b/Controllers/tagsController.cs @@ -0,0 +1,141 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.Entity; +using System.Linq; +using System.Net; +using System.Web; +using System.Web.Mvc; + +namespace WebApplication1.Models +{ + public class tagsController : Controller + { + private newswebappEntities db = new newswebappEntities(); + + // GET: tags + public ActionResult Index() + { + return View(db.tags.ToList()); + } + + // GET: tags/Details/5 + public ActionResult Details(int? id) + { + if (id == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + tag tags = db.tags.Find(id); + ViewBag.title = tags.title; + if (tags == null) + { + return HttpNotFound(); + } + //search for record that contains "tag title" in tag field. + return View(db.news.Where(n => n.tag.Contains(tags.title))); + } + + // GET: tags/Create + public ActionResult Create() + { + if (Session["User"] != null) + { + if (Convert.ToInt32(Session["Userrole"]) == 1) + { + return View(); + } + else + { + return new HttpStatusCodeResult(HttpStatusCode.Unauthorized); + } + } + else + return RedirectToAction("index", "home"); + } + + // POST: tags/Create + // To protect from overposting attacks, enable the specific properties you want to bind to, for + // more details see https://go.microsoft.com/fwlink/?LinkId=317598. + [HttpPost] + [ValidateAntiForgeryToken] + public ActionResult Create([Bind(Include = "code,link,title")] tag tag) + { + if (ModelState.IsValid) + { + tag.link = "tags/"+tag.title; + db.tags.Add(tag); + db.SaveChanges(); + return RedirectToAction("Index"); + } + + return View(tag); + } + + // GET: tags/Edit/5 + public ActionResult Edit(int? id) + { + if (id == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + tag tag = db.tags.Find(id); + if (tag == null) + { + return HttpNotFound(); + } + return View(tag); + } + + // POST: tags/Edit/5 + // To protect from overposting attacks, enable the specific properties you want to bind to, for + // more details see https://go.microsoft.com/fwlink/?LinkId=317598. + [HttpPost] + [ValidateAntiForgeryToken] + public ActionResult Edit([Bind(Include = "code,link,title")] tag tag) + { + if (ModelState.IsValid) + { + db.Entry(tag).State = EntityState.Modified; + db.SaveChanges(); + return RedirectToAction("Index"); + } + return View(tag); + } + + // GET: tags/Delete/5 + public ActionResult Delete(int? id) + { + if (id == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + tag tag = db.tags.Find(id); + if (tag == null) + { + return HttpNotFound(); + } + return View(tag); + } + + // POST: tags/Delete/5 + [HttpPost, ActionName("Delete")] + [ValidateAntiForgeryToken] + public ActionResult DeleteConfirmed(int id) + { + tag tag = db.tags.Find(id); + db.tags.Remove(tag); + db.SaveChanges(); + return RedirectToAction("Index"); + } + + protected override void Dispose(bool disposing) + { + if (disposing) + { + db.Dispose(); + } + base.Dispose(disposing); + } + } +} diff --git a/Views/Shared/_Layout.cshtml b/Views/Shared/_Layout.cshtml index c2408e5..dc8c731 100644 --- a/Views/Shared/_Layout.cshtml +++ b/Views/Shared/_Layout.cshtml @@ -1,4 +1,7 @@ - +@{ + string title = ViewBag.titlemenu; +} + @@ -21,9 +24,9 @@
  • @Html.ActionLink("About", "About", "Home", new { area = "" }, new { @class = "nav-link" })
  • @Html.ActionLink("Contact", "Contact", "Home", new { area = "" }, new { @class = "nav-link" })
  • @Html.ActionLink("user setting", "index", "users", new { area = "" }, new { @class = "nav-link" })
  • -
  • @Html.ActionLink("news", "index", "users", new { area = "" }, new { @class = "nav-link" })
  • +
  • @Html.ActionLink("news", "Index", "news", new { area = "" }, new { @class = "nav-link" })
  • @Html.ActionLink("categories", "index", "categories", new { area = "" }, new { @class = "nav-link" })
  • - +
  • @Html.ActionLink("tags", "index", "tags", new { area = "" }, new { @class = "nav-link" })
  • diff --git a/Views/categories/Create.cshtml b/Views/categories/Create.cshtml index a654788..0c8f4f9 100644 --- a/Views/categories/Create.cshtml +++ b/Views/categories/Create.cshtml @@ -22,10 +22,10 @@ @Html.ValidationMessageFor(model => model.title, "", new { @class = "text-danger" }) - +
    - +
    diff --git a/Views/categories/Delete.cshtml b/Views/categories/Delete.cshtml index 2a73e9a..1cb4dc4 100644 --- a/Views/categories/Delete.cshtml +++ b/Views/categories/Delete.cshtml @@ -33,7 +33,7 @@ @Html.AntiForgeryToken()
    - | + | @Html.ActionLink("Back to List", "Index")
    } diff --git a/Views/news/Create.cshtml b/Views/news/Create.cshtml index 8ef6239..9051af3 100644 --- a/Views/news/Create.cshtml +++ b/Views/news/Create.cshtml @@ -6,100 +6,86 @@

    Create

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

    news

    -
    - @Html.ValidationSummary(true, "", new { @class = "text-danger" }) -
    - @Html.LabelFor(model => model.title, htmlAttributes: new { @class = "control-label col-md-2" }) -
    - @Html.EditorFor(model => model.title, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.title, "", new { @class = "text-danger" }) -
    -
    +
    +

    news

    +
    + @Html.ValidationSummary(true, "", new { @class = "text-danger" }) -
    - @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" }) -
    - @Html.EditorFor(model => model.link, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.link, "", new { @class = "text-danger" }) -
    -
    - -
    - @Html.LabelFor(model => model.summary, 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.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" }) -
    -
    - -
    - @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.EditorFor(model => model.publishDate, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.publishDate, "", 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.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" }) -
    -
    - -
    -
    - -
    +
    + @Html.LabelFor(model => model.title, htmlAttributes: new { @class = "control-label col-md-2" }) +
    + @Html.EditorFor(model => model.title, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.title, "", new { @class = "text-danger" })
    + +
    + @Html.LabelFor(model => model.link, htmlAttributes: new { @class = "control-label col-md-2" }) +
    + @Html.EditorFor(model => model.link, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.link, "", new { @class = "text-danger" }) +
    +
    + +
    + @Html.LabelFor(model => model.image, htmlAttributes: new { @class = "control-label col-md-2" }) +
    + @Html.EditorFor(model => model.image, new { htmlAttributes = new { @type = "file", @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.image, "", new { @class = "text-danger" }) +
    +
    +
    + +
    + + @Html.ValidationMessageFor(model => model.cat, "", new { @class = "text-danger" }) +
    +
    + +
    + @Html.LabelFor(model => model.tag, htmlAttributes: new { @class = "control-label col-md-2" }) +
    + + @Html.ValidationMessageFor(model => model.tag, "", new { @class = "text-danger" }) +
    +
    + +
    + @Html.LabelFor(model => model.summary, htmlAttributes: new { @class = "control-label col-md-2" }) +
    + @Html.TextAreaFor(model => model.summary, new { @class = "form-control" }) + @Html.ValidationMessageFor(model => model.summary, "", new { @class = "text-danger" }) +
    +
    +
    + @Html.LabelFor(model => model.Content, htmlAttributes: new { @class = "control-label col-md-2" }) +
    + @Html.TextAreaFor(model => model.Content, new {@class = "form-control"} ) + @Html.ValidationMessageFor(model => model.Content, "", new { @class = "text-danger" }) +
    +
    +
    + +
    +
    + +
    +
    +
    }
    diff --git a/Views/tags/Create.cshtml b/Views/tags/Create.cshtml new file mode 100644 index 0000000..25bf47a --- /dev/null +++ b/Views/tags/Create.cshtml @@ -0,0 +1,36 @@ +@model WebApplication1.Models.tag + +@{ + ViewBag.Title = "Create"; +} + +

    Create

    + +@using (Html.BeginForm()) +{ + @Html.AntiForgeryToken() + +
    +

    tag

    +
    + @Html.ValidationSummary(true, "", new { @class = "text-danger" }) + +
    + @Html.LabelFor(model => model.title, htmlAttributes: new { @class = "control-label col-md-2" }) +
    + @Html.EditorFor(model => model.title, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.title, "", new { @class = "text-danger" }) +
    +
    +
    +
    +
    + +
    +
    +
    +} + +
    + @Html.ActionLink("Back to List", "Index") +
    diff --git a/Views/tags/Delete.cshtml b/Views/tags/Delete.cshtml new file mode 100644 index 0000000..96281a8 --- /dev/null +++ b/Views/tags/Delete.cshtml @@ -0,0 +1,40 @@ +@model WebApplication1.Models.tag + +@{ + ViewBag.Title = "Delete"; +} + +

    Delete

    + +

    Are you sure you want to delete this?

    +
    +

    tag

    +
    +
    +
    + @Html.DisplayNameFor(model => model.link) +
    + +
    + @Html.DisplayFor(model => model.link) +
    + +
    + @Html.DisplayNameFor(model => model.title) +
    + +
    + @Html.DisplayFor(model => model.title) +
    + +
    + + @using (Html.BeginForm()) { + @Html.AntiForgeryToken() + +
    + | + @Html.ActionLink("Back to List", "Index") +
    + } +
    diff --git a/Views/tags/Details.cshtml b/Views/tags/Details.cshtml new file mode 100644 index 0000000..08a37c8 --- /dev/null +++ b/Views/tags/Details.cshtml @@ -0,0 +1,49 @@ +@model IEnumerable + +@{ + ViewBag.Title = @ViewBag.title; +} + +

    @ViewBag.title

    + + + + + + + + + + + @foreach (var item in Model) + { + + + + + + + + } + +
    + @Html.DisplayNameFor(model => model.title) + + @Html.DisplayNameFor(model => model.link) + + @Html.DisplayNameFor(model => model.tag) + + tags +
    + @Html.DisplayFor(modelItem => item.title) + + @Html.DisplayFor(modelItem => item.link) + + @Html.DisplayFor(modelItem => item.tag) + + @Html.DisplayFor(modelItem => item.cat) + + @Html.ActionLink("Edit", "Edit", new { id = item.ID }) | + @Html.ActionLink("Details", "Details", new { id = item.ID }) | + @Html.ActionLink("Delete", "Delete", new { id = item.ID }) +
    diff --git a/Views/tags/Edit.cshtml b/Views/tags/Edit.cshtml new file mode 100644 index 0000000..91fe0b2 --- /dev/null +++ b/Views/tags/Edit.cshtml @@ -0,0 +1,45 @@ +@model WebApplication1.Models.tag + +@{ + ViewBag.Title = "Edit"; +} + +

    Edit

    + +@using (Html.BeginForm()) +{ + @Html.AntiForgeryToken() + +
    +

    tag

    +
    + @Html.ValidationSummary(true, "", new { @class = "text-danger" }) + @Html.HiddenFor(model => model.code) + +
    + @Html.LabelFor(model => model.link, htmlAttributes: new { @class = "control-label col-md-2" }) +
    + @Html.EditorFor(model => model.link, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.link, "", new { @class = "text-danger" }) +
    +
    + +
    + @Html.LabelFor(model => model.title, htmlAttributes: new { @class = "control-label col-md-2" }) +
    + @Html.EditorFor(model => model.title, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.title, "", new { @class = "text-danger" }) +
    +
    + +
    +
    + +
    +
    +
    +} + +
    + @Html.ActionLink("Back to List", "Index") +
    diff --git a/Views/tags/Index.cshtml b/Views/tags/Index.cshtml new file mode 100644 index 0000000..ad89d25 --- /dev/null +++ b/Views/tags/Index.cshtml @@ -0,0 +1,39 @@ +@model IEnumerable + +@{ + ViewBag.Title = "Index"; +} + +

    Index

    + +

    + @Html.ActionLink("Create New", "Create") +

    + + + + + + + +@foreach (var item in Model) { + + + + + +} + +
    + @Html.DisplayNameFor(model => model.link) + + @Html.DisplayNameFor(model => model.title) +
    + @Html.DisplayFor(modelItem => item.link) + + @Html.DisplayFor(modelItem => item.title) + + @Html.ActionLink("Edit", "Edit", new { id=item.code }) | + @Html.ActionLink("Details", "Details", new { id=item.code }) | + @Html.ActionLink("Delete", "Delete", new { id=item.code }) +
    diff --git a/WebApplication1.csproj b/WebApplication1.csproj index a666ed9..b4f5157 100644 --- a/WebApplication1.csproj +++ b/WebApplication1.csproj @@ -154,6 +154,7 @@ + True @@ -268,6 +269,11 @@ + + + + +