Click to See Complete Forum and Search --> : [RESOLVED] Decrement displaying of data
azsx123
May 20th, 2011, 10:54 AM
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
<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 })?
gep13
May 21st, 2011, 09:00 AM
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
Krokonoster
May 21st, 2011, 11:22 AM
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)
azsx123
May 21st, 2011, 11:34 AM
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.
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!
Krokonoster
May 21st, 2011, 12:03 PM
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.
azsx123
May 21st, 2011, 12:30 PM
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
Krokonoster
May 21st, 2011, 12:32 PM
Then where does that code you posted come from? That ain't part of the default template.
azsx123
May 21st, 2011, 12:35 PM
Are you pertaining to the controller?
// GET: /News/
public ViewResult Index()
{
return View(db.BatchNews.ToList());
}
I've been looking for the quries myself but am unable to do so.
Krokonoster
May 21st, 2011, 12:42 PM
Pseudo code, trust you can figure it out:
IList<NewsItem> items = (from item in db.BatchNews
orderby item.NewsDate
select item).ToList();
return View(items);
azsx123
May 21st, 2011, 12:43 PM
I do. Thanks so much! =)
Edit: Its not working.
public ViewResult Index()
{
IList<NewsModel> items = (from item in db.BatchNews
orderby item.ID descending
select item).ToList();
return View(db.BatchNews.ToList());
}
gep13
May 24th, 2011, 01:02 AM
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
Krokonoster
May 24th, 2011, 02:48 AM
I do. Thanks so much! =)
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).
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.