display news list and individual
This commit is contained in:
@ -15,14 +15,29 @@ namespace WebApplication1.Controllers
|
||||
public class newsController : Controller
|
||||
{
|
||||
private newswebappEntities db = new newswebappEntities();
|
||||
|
||||
|
||||
// GET: news
|
||||
public ActionResult Index()
|
||||
{
|
||||
var news = db.news.ToList();
|
||||
var users = db.users.ToList();
|
||||
var models = new Tuple<List<news>, List<user>>(news, users);
|
||||
return View(db.news.ToList());
|
||||
var models = (from user in db.users // Access Users DbSet
|
||||
join news in db.news on user.ID equals news.userID into newsGroup // Join News DbSet
|
||||
from news in newsGroup.DefaultIfEmpty() // Left outer join
|
||||
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
|
||||
@ -38,7 +53,7 @@ namespace WebApplication1.Controllers
|
||||
return HttpNotFound();
|
||||
}
|
||||
var user = db.users.Find(news.userID);
|
||||
// var model = new Tuple<object,object>(news,user);
|
||||
ViewBag.user = user;
|
||||
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";
|
||||
var _user = ViewBag.user as user;
|
||||
}
|
||||
|
||||
<h2>Details</h2>
|
||||
@ -23,8 +25,8 @@
|
||||
</dt>
|
||||
|
||||
<dd>
|
||||
<img src="@Model.image" />
|
||||
@Html.DisplayFor(model => model.image)
|
||||
<img src="@Url.Content(Model.image)" height="250" width="200" />
|
||||
|
||||
</dd>
|
||||
|
||||
<dt>
|
||||
@ -68,11 +70,11 @@
|
||||
</dd>
|
||||
|
||||
<dt>
|
||||
@Html.DisplayNameFor(model => model.userID)
|
||||
original poster
|
||||
</dt>
|
||||
|
||||
<dd>
|
||||
@Html.DisplayFor(model => model.userID)
|
||||
@_user.displayname
|
||||
</dd>
|
||||
|
||||
<dt>
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
@model IEnumerable<WebApplication1.Models.news>
|
||||
@using WebApplication1.Models
|
||||
@model List<newsModel>
|
||||
@{
|
||||
ViewBag.Title = "Index";
|
||||
}
|
||||
@ -11,19 +12,22 @@
|
||||
<table class="table">
|
||||
<tr>
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.title)
|
||||
Title
|
||||
</th>
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.cat)
|
||||
Category
|
||||
</th>
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.publishDate)
|
||||
Tag
|
||||
</th>
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.userID)
|
||||
Publisher
|
||||
</th>
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.views)
|
||||
Published On
|
||||
</th>
|
||||
<th>
|
||||
views
|
||||
</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
@ -31,24 +35,27 @@
|
||||
@foreach (var item in Model) {
|
||||
<tr>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.title)
|
||||
@Html.DisplayFor(modelItem => item.Title)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.cat)
|
||||
@Html.DisplayFor(modelItem => item.category)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.publishDate)
|
||||
@Html.DisplayFor(modelItem => item.tag)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.userID)
|
||||
@Html.DisplayFor(modelItem => item.DisplayName)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.date)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.views)
|
||||
</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 })
|
||||
@Html.ActionLink("Edit", "Edit", new { id = item.NewsID }) |
|
||||
@Html.ActionLink("Details", "Details", new { id = item.NewsID }) |
|
||||
@Html.ActionLink("Delete", "Delete", new { id = item.NewsID })
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
|
||||
@ -152,6 +152,7 @@
|
||||
<Compile Include="Models\category.cs">
|
||||
<DependentUpon>Model1.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Models\newsModel.cs" />
|
||||
<Compile Include="Models\Login.cs" />
|
||||
<Compile Include="Models\Register.cs" />
|
||||
<Compile Include="Controllers\tagsController.cs" />
|
||||
|
||||
Reference in New Issue
Block a user