[RESOLVED] Decrement displaying of data
I want to make a "announcements" page and what I want to happen is that when a user posts a news, the post will be saved into the database and is defined by a unique ID (using autonumber). So data is arranged by that.
When a user loads the page, announcements will be displayed from lastest to oldest.
So what I'm trying to ask here is how can I list data from latest to oldest.
The container of the announcements is below
Code:
<table >
<tr>
<th>
Date Posted
</th>
<th>
Author
</th>
<th>
Title
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.NewsDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Author)
</td>
<td>
@Html.DisplayFor(modelItem => item.NewsTitle)
</td>
<td>
@Html.ActionLink("Read", "Details", new { id=item.ID })
</td>
</tr>
}
Edit: Is there a way that " @Html.DisplayFor(modelItem => item.NewsTitle)" will funtion as a link similar to @Html.ActionLink("Read", "Details", new { id=item.ID })?
Re: Decrement displaying of data
Hello,
Looks to me like you are doing ASP.Net MVC, so I am going to more this question over to the ASP.Net MVC forum.
Gary
Re: Decrement displaying of data
Order the data when you retrieve it from your repository. (How depends how you query the data. LINQ in most my work. orderby dateValue descending)
As for the 2nd question
@Html.ActionLink(item.NewsTitle, "Read", "Details", new { id=item.ID })
(think the parameters is displaytext, action, controller, attributes)
Re: Decrement displaying of data
Quote:
Originally Posted by
Krokonoster
Order the data when you retrieve it from your repository. (How depends how you query the data. LINQ in most my work. orderby dateValue descending)
I'm using Sql server ce.
Quote:
Originally Posted by
Krokonoster
As for the 2nd question
@Html.ActionLink(item.NewsTitle, "Read", "Details", new { id=item.ID })
(think the parameters is displaytext, action, controller, attributes)
I see. Thanks!
Re: Decrement displaying of data
Do you have a domain model hooked up to your database (the latter not being important here)?
How you query the domain model then? LINQ?
Or do you directly query the database in your controller (hope not).
Either case you can use orderby.
Re: Decrement displaying of data
I don't use queries. None as of I know of. I i'm using the login function that came with the MVC 3 layout
Re: Decrement displaying of data
Then where does that code you posted come from? That ain't part of the default template.
Re: Decrement displaying of data
Are you pertaining to the controller?
Code:
// GET: /News/
public ViewResult Index()
{
return View(db.BatchNews.ToList());
}
I've been looking for the quries myself but am unable to do so.
Re: Decrement displaying of data
Pseudo code, trust you can figure it out:
Code:
IList<NewsItem> items = (from item in db.BatchNews
orderby item.NewsDate
select item).ToList();
return View(items);
Re: Decrement displaying of data
I do. Thanks so much! =)
Edit: Its not working.
Code:
public ViewResult Index()
{
IList<NewsModel> items = (from item in db.BatchNews
orderby item.ID descending
select item).ToList();
return View(db.BatchNews.ToList());
}
Re: [RESOLVED] Decrement displaying of data
Hello,
When you say something is not working, can you please provide some more information?
i.e. what exactly isn't working, and what steps have you taken to test it.
Was there an exception? If so, what was it? Did it not compile? If not, what was the error?
In order to best help you out, you need to help us out by providing as much information as possible.
Gary
Re: Decrement displaying of data
Quote:
Originally Posted by
azsx123
I do. Thanks so much! =)
Code:
public ViewResult Index()
{
IList<NewsModel> items = (from item in db.BatchNews
orderby item.ID descending
select item).ToList();
return View(db.BatchNews.ToList());
}
return View(items);
Second on what Gary said. Think carefully what information others will need to help you and include that. (That does not mean posting 100's lines of irrelevant code).