tag pages

news create form correction
This commit is contained in:
2024-07-29 14:54:26 +03:30
parent a74072b491
commit b68d3f3185
14 changed files with 467 additions and 98 deletions

View File

@ -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";
}
}
}

View File

@ -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

View File

@ -38,8 +38,24 @@ namespace WebApplication1.Controllers
// GET: news/Create
public ActionResult Create()
{
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<category>, List<tag>>(cat, tags);
return View();
}
if (Session["User"] != null)
{
ViewBag.categoryTitle = db.categories.ToList();
return View();
}
else
return RedirectToAction("login", "users");
}
// POST: news/Create
// To protect from overposting attacks, enable the specific properties you want to bind to, for
@ -50,6 +66,7 @@ namespace WebApplication1.Controllers
{
if (ModelState.IsValid)
{
db.news.Add(news);
db.SaveChanges();
return RedirectToAction("Index");

View File

@ -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);
}
}
}

View File

@ -1,4 +1,7 @@
<!DOCTYPE html>
@{
string title = ViewBag.titlemenu;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
@ -21,9 +24,9 @@
<li>@Html.ActionLink("About", "About", "Home", new { area = "" }, new { @class = "nav-link" })</li>
<li>@Html.ActionLink("Contact", "Contact", "Home", new { area = "" }, new { @class = "nav-link" })</li>
<li>@Html.ActionLink("user setting", "index", "users", new { area = "" }, new { @class = "nav-link" })</li>
<li>@Html.ActionLink("news", "index", "users", new { area = "" }, new { @class = "nav-link" })</li>
<li>@Html.ActionLink("news", "Index", "news", new { area = "" }, new { @class = "nav-link" })</li>
<li>@Html.ActionLink("categories", "index", "categories", new { area = "" }, new { @class = "nav-link" })</li>
<li>@Html.ActionLink("tags", "index", "tags", new { area = "" }, new { @class = "nav-link" })</li>
</ul>
</div>
</div>

View File

@ -22,10 +22,10 @@
@Html.ValidationMessageFor(model => model.title, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
<input type="submit" value="Create" class="btn btn-outline-dark" />
</div>
</div>
</div>

View File

@ -33,7 +33,7 @@
@Html.AntiForgeryToken()
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-default" /> |
<input type="submit" value="Delete" class="btn btn-outline-dark" /> |
@Html.ActionLink("Back to List", "Index")
</div>
}

View File

@ -6,14 +6,15 @@
<h2>Create</h2>
@using (Html.BeginForm())
@using (Html.BeginForm(new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<div class="form">
<h4>news</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@ -22,14 +23,6 @@
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.image, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.image, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.image, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.link, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@ -39,17 +32,21 @@
</div>
<div class="form-group">
@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" })
<div class="col-md-10">
@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" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.cat, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.cat, new { htmlAttributes = new { @class = "form-control" } })
<label class="control-label col-md-2">category</label>
<div>
<select id="cat" name="cat" class="form-control" multiple>
@foreach (var item in ViewBag.categoryTitle)
{
<option value= "@item.title">@item.title</option>
}
</select>
@Html.ValidationMessageFor(model => model.cat, "", new { @class = "text-danger" })
</div>
</div>
@ -57,46 +54,35 @@
<div class="form-group">
@Html.LabelFor(model => model.tag, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.tag, new { htmlAttributes = new { @class = "form-control" } })
<select id="cat" name="cat" class="form-control" multiple>
@foreach (var item in ViewBag.tagTitle)
{
<option value="@item.title">@item.title</option>
}
</select>
@Html.ValidationMessageFor(model => model.tag, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@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" })
<div class="col-md-10">
@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" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.userID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.userID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.userID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Content, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@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" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.views, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.views, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.views, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
<input type="submit" value="Create" class="btn btn-outline-dark" />
</div>
</div>
</div>

36
Views/tags/Create.cshtml Normal file
View File

@ -0,0 +1,36 @@
@model WebApplication1.Models.tag
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>tag</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.title, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-outline-dark" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>

40
Views/tags/Delete.cshtml Normal file
View File

@ -0,0 +1,40 @@
@model WebApplication1.Models.tag
@{
ViewBag.Title = "Delete";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>tag</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.link)
</dt>
<dd>
@Html.DisplayFor(model => model.link)
</dd>
<dt>
@Html.DisplayNameFor(model => model.title)
</dt>
<dd>
@Html.DisplayFor(model => model.title)
</dd>
</dl>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-default" /> |
@Html.ActionLink("Back to List", "Index")
</div>
}
</div>

49
Views/tags/Details.cshtml Normal file
View File

@ -0,0 +1,49 @@
@model IEnumerable<WebApplication1.Models.news>
@{
ViewBag.Title = @ViewBag.title;
}
<h2>@ViewBag.title</h2>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.title)
</th>
<th>
@Html.DisplayNameFor(model => model.link)
</th>
<th>
@Html.DisplayNameFor(model => model.tag)
</th>
<th>
tags
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.title)
</td>
<td>
@Html.DisplayFor(modelItem => item.link)
</td>
<td>
@Html.DisplayFor(modelItem => item.tag)
</td>
<td>
@Html.DisplayFor(modelItem => item.cat)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
@Html.ActionLink("Details", "Details", new { id = item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ID })
</td>
</tr>
}
</table>

45
Views/tags/Edit.cshtml Normal file
View File

@ -0,0 +1,45 @@
@model WebApplication1.Models.tag
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>tag</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.code)
<div class="form-group">
@Html.LabelFor(model => model.link, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.link, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.link, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>

39
Views/tags/Index.cshtml Normal file
View File

@ -0,0 +1,39 @@
@model IEnumerable<WebApplication1.Models.tag>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.link)
</th>
<th>
@Html.DisplayNameFor(model => model.title)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.link)
</td>
<td>
@Html.DisplayFor(modelItem => item.title)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.code }) |
@Html.ActionLink("Details", "Details", new { id=item.code }) |
@Html.ActionLink("Delete", "Delete", new { id=item.code })
</td>
</tr>
}
</table>

View File

@ -154,6 +154,7 @@
</Compile>
<Compile Include="Models\Login.cs" />
<Compile Include="Models\Register.cs" />
<Compile Include="Controllers\tagsController.cs" />
<Compile Include="Models\TheNews.cs" />
<Compile Include="Models\Model1.Context.cs">
<AutoGen>True</AutoGen>
@ -268,6 +269,11 @@
<Content Include="Views\news\Details.cshtml" />
<Content Include="Views\news\Edit.cshtml" />
<Content Include="Views\news\Index.cshtml" />
<Content Include="Views\tags\Create.cshtml" />
<Content Include="Views\tags\Delete.cshtml" />
<Content Include="Views\tags\Details.cshtml" />
<Content Include="Views\tags\Edit.cshtml" />
<Content Include="Views\tags\Index.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />