display news list and individual
This commit is contained in:
@ -15,14 +15,29 @@ 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 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/Details/5
|
// GET: news/Details/5
|
||||||
@ -38,7 +53,7 @@ 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;
|
||||||
return View(news);
|
return View(news);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
19
Models/newsModel.cs
Normal file
19
Models/newsModel.cs
Normal 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 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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,7 +1,9 @@
|
|||||||
@model WebApplication1.Models.news
|
@using WebApplication1.Models
|
||||||
|
@model news
|
||||||
|
|
||||||
@{
|
@{
|
||||||
ViewBag.Title = "Details";
|
ViewBag.Title = "Details";
|
||||||
|
var _user = ViewBag.user as user;
|
||||||
}
|
}
|
||||||
|
|
||||||
<h2>Details</h2>
|
<h2>Details</h2>
|
||||||
@ -23,8 +25,8 @@
|
|||||||
</dt>
|
</dt>
|
||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
<img src="@Model.image" />
|
<img src="@Url.Content(Model.image)" height="250" width="200" />
|
||||||
@Html.DisplayFor(model => model.image)
|
|
||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
<dt>
|
<dt>
|
||||||
@ -68,11 +70,11 @@
|
|||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
<dt>
|
<dt>
|
||||||
@Html.DisplayNameFor(model => model.userID)
|
original poster
|
||||||
</dt>
|
</dt>
|
||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
@Html.DisplayFor(model => model.userID)
|
@_user.displayname
|
||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
<dt>
|
<dt>
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
@model IEnumerable<WebApplication1.Models.news>
|
@using WebApplication1.Models
|
||||||
|
@model List<newsModel>
|
||||||
@{
|
@{
|
||||||
ViewBag.Title = "Index";
|
ViewBag.Title = "Index";
|
||||||
}
|
}
|
||||||
@ -11,19 +12,22 @@
|
|||||||
<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)
|
Publisher
|
||||||
</th>
|
</th>
|
||||||
<th>
|
<th>
|
||||||
@Html.DisplayNameFor(model => model.views)
|
Published On
|
||||||
|
</th>
|
||||||
|
<th>
|
||||||
|
views
|
||||||
</th>
|
</th>
|
||||||
<th></th>
|
<th></th>
|
||||||
</tr>
|
</tr>
|
||||||
@ -31,24 +35,27 @@
|
|||||||
@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.DisplayName)
|
||||||
|
</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.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>
|
||||||
}
|
}
|
||||||
|
|||||||
@ -152,6 +152,7 @@
|
|||||||
<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" />
|
||||||
|
|||||||
Reference in New Issue
Block a user