I have run through the MVC3 Razor tutorial on ASP.Net, and and have started trying adapt it a bit to work with a small web project of my own.
What I have done so far is create a section that will add two fields to a new database record. A “News_Item” and a “News_Date” field.
Then when displaying this data on a separate page I only want to display the 4 most recently added records.
My View code (below) displays all entries in the database no problem, but I’d like to amend as stated above so it only displays the 4 most recent records. How would I go about doing this please? I am a beginner, and have no real prior experience with development.
In my Controller I have the following code for “Get”:
public ViewResult Index()
{
return View(db.News_Entries.ToList());
}
and just in case it is needed, here is the Model code:
namespace dale_harrison.Models
{
public class News
{
public int ID { get; set; }
public string News_Entry { get; set; }
public DateTime News_Date { get; set; }
}
public class NewsDBContext : DbContext
{
public DbSet<News> News_Entries { get; set; }
}
}
Many thanks for any help.
Change your Actions code
you should read linq to entity tutorials