Revert "display news list and individual"

This reverts commit 2002c9fe09.
This commit is contained in:
2024-08-01 15:05:21 +03:30
parent 2002c9fe09
commit c31a2eab12
5 changed files with 23 additions and 67 deletions

View File

@ -15,29 +15,14 @@ namespace WebApplication1.Controllers
public class newsController : Controller public class newsController : Controller
{ {
private newswebappEntities db = new newswebappEntities(); private newswebappEntities db = new newswebappEntities();
// GET: news // GET: news
public ActionResult Index() public ActionResult Index()
{ {
var models = (from user in db.users // Access Users DbSet var news = db.news.ToList();
join news in db.news on user.ID equals news.userID into newsGroup // Join News DbSet var users = db.users.ToList();
from news in newsGroup.DefaultIfEmpty() // Left outer join var models = new Tuple<List<news>, List<user>>(news, users);
where news != null // Filter based on existence of news record return View(db.news.ToList());
select new newsModel
{
DisplayName = user != null ? user.displayname : null,
NewsID = news.ID, // Use null-conditional operator for missing news
Title = news.title,
Image = news.image,
category = news.cat,
tag = news.tag,
date = news.publishDate,
views = news.views
}).ToList();
return View(models);
} }
// GET: news/Details/5 // GET: news/Details/5
@ -53,7 +38,7 @@ namespace WebApplication1.Controllers
return HttpNotFound(); return HttpNotFound();
} }
var user = db.users.Find(news.userID); var user = db.users.Find(news.userID);
ViewBag.user = user; // var model = new Tuple<object,object>(news,user);
return View(news); return View(news);
} }

View File

@ -1,19 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1.Models
{
public class newsModel
{
public string DisplayName { get; set; }
public string Title { get; set; }
public int? NewsID { get; set; }
public string Image { get; set; }
public string category { get; set; }
public string tag { get; set; }
public int? views { get; set; }
public DateTime? date { get; set; }
}
}

View File

@ -1,9 +1,7 @@
@using WebApplication1.Models @model WebApplication1.Models.news
@model news
@{ @{
ViewBag.Title = "Details"; ViewBag.Title = "Details";
var _user = ViewBag.user as user;
} }
<h2>Details</h2> <h2>Details</h2>
@ -25,8 +23,8 @@
</dt> </dt>
<dd> <dd>
<img src="@Url.Content(Model.image)" height="250" width="200" /> <img src="@Model.image" />
@Html.DisplayFor(model => model.image)
</dd> </dd>
<dt> <dt>
@ -70,11 +68,11 @@
</dd> </dd>
<dt> <dt>
original poster @Html.DisplayNameFor(model => model.userID)
</dt> </dt>
<dd> <dd>
@_user.displayname @Html.DisplayFor(model => model.userID)
</dd> </dd>
<dt> <dt>

View File

@ -1,5 +1,4 @@
@using WebApplication1.Models @model IEnumerable<WebApplication1.Models.news>
@model List<newsModel>
@{ @{
ViewBag.Title = "Index"; ViewBag.Title = "Index";
} }
@ -12,22 +11,19 @@
<table class="table"> <table class="table">
<tr> <tr>
<th> <th>
Title @Html.DisplayNameFor(model => model.title)
</th> </th>
<th> <th>
Category @Html.DisplayNameFor(model => model.cat)
</th> </th>
<th> <th>
Tag @Html.DisplayNameFor(model => model.publishDate)
</th> </th>
<th> <th>
Publisher @Html.DisplayNameFor(model => model.userID)
</th> </th>
<th> <th>
Published On @Html.DisplayNameFor(model => model.views)
</th>
<th>
views
</th> </th>
<th></th> <th></th>
</tr> </tr>
@ -35,27 +31,24 @@
@foreach (var item in Model) { @foreach (var item in Model) {
<tr> <tr>
<td> <td>
@Html.DisplayFor(modelItem => item.Title) @Html.DisplayFor(modelItem => item.title)
</td> </td>
<td> <td>
@Html.DisplayFor(modelItem => item.category) @Html.DisplayFor(modelItem => item.cat)
</td> </td>
<td> <td>
@Html.DisplayFor(modelItem => item.tag) @Html.DisplayFor(modelItem => item.publishDate)
</td> </td>
<td> <td>
@Html.DisplayFor(modelItem => item.DisplayName) @Html.DisplayFor(modelItem => item.userID)
</td>
<td>
@Html.DisplayFor(modelItem => item.date)
</td> </td>
<td> <td>
@Html.DisplayFor(modelItem => item.views) @Html.DisplayFor(modelItem => item.views)
</td> </td>
<td> <td>
@Html.ActionLink("Edit", "Edit", new { id = item.NewsID }) | @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id = item.NewsID }) | @Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.NewsID }) @Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td> </td>
</tr> </tr>
} }

View File

@ -152,7 +152,6 @@
<Compile Include="Models\category.cs"> <Compile Include="Models\category.cs">
<DependentUpon>Model1.tt</DependentUpon> <DependentUpon>Model1.tt</DependentUpon>
</Compile> </Compile>
<Compile Include="Models\newsModel.cs" />
<Compile Include="Models\Login.cs" /> <Compile Include="Models\Login.cs" />
<Compile Include="Models\Register.cs" /> <Compile Include="Models\Register.cs" />
<Compile Include="Controllers\tagsController.cs" /> <Compile Include="Controllers\tagsController.cs" />