show currect news list and user name in news detals

This commit is contained in:
2024-08-01 16:35:13 +03:30
parent 075e75e62d
commit d73b5dc266
5 changed files with 101 additions and 38 deletions

View File

@ -19,10 +19,29 @@ namespace WebApplication1.Controllers
// GET: news // GET: news
public ActionResult Index() public ActionResult Index()
{ {
var news = db.news.ToList(); var models = (from user in db.users // Access Users DbSet
var users = db.users.ToList(); join news in db.news on user.ID equals news.userID into newsGroup // Join News DbSet
var models = new Tuple<List<news>, List<user>>(news, users); from news in newsGroup.DefaultIfEmpty() // Left outer join
return View(db.news.ToList()); where news != null // Filter based on existence of news record
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
public ActionResult Latest()
{
return View(db.news.OrderByDescending(v => v.views).ToList());
} }
// GET: news/Details/5 // GET: news/Details/5
@ -38,7 +57,21 @@ namespace WebApplication1.Controllers
return HttpNotFound(); return HttpNotFound();
} }
var user = db.users.Find(news.userID); var user = db.users.Find(news.userID);
// var model = new Tuple<object,object>(news,user); ViewBag.user = user;
news.views += 1;
db.SaveChanges();
/*
var key = Request.ServerVariables["REMOTE_ADDR"];
var now = DateTime.UtcNow;
if (now - View.LastVisited > _timeLimit)
{
pageView.LastVisited = now;
_pageViews[key] = pageView;
return true; // View counted
}
return false; // View within time limit, not counted
*/
return View(news); return View(news);
} }

19
Models/newsModel.cs Normal file
View File

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

View File

@ -2,6 +2,7 @@
@{ @{
ViewBag.Title = "Details"; ViewBag.Title = "Details";
} }
<h2>Details</h2> <h2>Details</h2>
@ -23,8 +24,7 @@
</dt> </dt>
<dd> <dd>
<img src="@Model.image" /> <img src="@Url.Content(Model.image)" height="270" width="200" />
@Html.DisplayFor(model => model.image)
</dd> </dd>
<dt> <dt>
@ -72,7 +72,7 @@
</dt> </dt>
<dd> <dd>
@Html.DisplayFor(model => model.userID) @ViewBag.user.displayname
</dd> </dd>
<dt> <dt>

View File

@ -1,4 +1,7 @@
@model IEnumerable<WebApplication1.Models.news> @using WebApplication1.Models
@model IEnumerable<newsModel>
@{ @{
ViewBag.Title = "Index"; ViewBag.Title = "Index";
} }
@ -11,46 +14,53 @@
<table class="table"> <table class="table">
<tr> <tr>
<th> <th>
@Html.DisplayNameFor(model => model.title) title
</th> </th>
<th> <th>
@Html.DisplayNameFor(model => model.cat) category
</th> </th>
<th> <th>
@Html.DisplayNameFor(model => model.publishDate) tag
</th> </th>
<th> <th>
@Html.DisplayNameFor(model => model.userID) publish date
</th> </th>
<th> <th>
@Html.DisplayNameFor(model => model.views) publisher
</th>
<th>
Views
</th> </th>
<th></th> <th></th>
</tr> </tr>
@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.cat) @Html.DisplayFor(modelItem => item.category)
</td> </td>
<td> <td>
@Html.DisplayFor(modelItem => item.publishDate) @Html.DisplayFor(modelItem => item.tag)
</td> </td>
<td> <td>
@Html.DisplayFor(modelItem => item.userID) @Html.DisplayFor(modelItem => item.date)
</td>
<td>
@Html.DisplayFor(modelItem => item.DisplayName)
</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.ID }) | @Html.ActionLink("Edit", "Edit", new { id = item.NewsID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) | @Html.ActionLink("Details", "Details", new { id = item.NewsID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID }) @Html.ActionLink("Delete", "Delete", new { id = item.NewsID })
</td> </td>
</tr> </tr>
} }
</table> </table>

View File

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