tags:

views:

115

answers:

4

I have developed a news page for a website in ASP.NET MVC and its URL being localhost/News. For this purpose I have developed a controller, model, and a view that iterates over all items in a model and displays them. This page displays all the News available in database in descending order of date. I have used routing News/{id} for displaying that particular news in details.

What should I do, if I want to display say only last 5 news posts on my home page? Can the controller be reused? Or I'll need to rewrite the code for fetching news records from my database?

+1  A: 

The way I would approach this is to have a View that takes an IQueryable list of news items.

I would then have a PartialView which renders only a single news item.

My View would then itterate through the IQueryable collection and render all news items that it finds there using foreach (NewsItem item in Model){}. Each news item will be rendered in the PartialView.

At this point that satifies the requirement to render news items. If you now only wanted to show the top 5 then I'd change my route to be something like News/{top}. That way I could pass in a number to the controller to bring back the top 5.

@LukLed's advice on using Take it the correct way if you are using LINQ. If not then use your own algorithym to get the top 5 posts.

Anyway, then in your Index view you could have a link that says, and this is untested, something like;

<%= Html.ActionLink("Top 5", "Index", new { top=5 }) %>

Not sure if the above is right but you get the idea.

Now you can have several links that allow for top n posts.

griegs
Thank for comments. Let me explain the scenario in which I want to use it. I want to display the recent news(hence top 5) on the bottom right page of a typical corporate home page. Also I have a link at the bottom of recent news saying "More News" that goes to www.domain.com/News/. It displays all the news in Database.So I want 5 hyperlinks for displaying top 5 on the homepage itself. My only question do I have to use Database Context on Homepage again or I can use the something like News/Top/5 from homepage(just like service call). If latter one is possible, then I want to know how to do it
H Sampat
A: 

I got some ideas after doing some search. I want to know can these be helpful :

  1. Using Partial View so that it follows DRY Principle
  2. Designing a method with JsonResult as a return type and using it with jquery.

Any other suggestions are welcome.

H Sampat
A: 

Your comment on using Partial Views with jQuery makes a lot of sense.

I never really understood why you wanted to reuse your Actions.

Doing what you want with partial views and jQuery is how others are doing it. It's quick and it looks fancy and slick.

griegs
+1  A: 

I recently had pretty much the same question. I've ended up using MVC Futures RenderAction implementation. A good example explaining how to use it is here:

http://eduncan911.com/blog/html-renderaction-for-asp-net-mvc-1-0.aspx

Basically you'd have something like this in your ArticleController (I don't call mine "NewsController" simply because I'm paranoid about it assuming "News" is a collection of type "New" :P)...

public ActionResult SidebarBox()
{
// I use a repository pattern to get records, just replace it with whatever you use
// also replace *.Take(5)* with however many you want returned
return View(repoArticles.GetAllArticles().Take(5).ToList());
}

and in your Master Page (or wherever you want to display it) you'd use:

<% Html.RenderAction<ArticleController>(c => c.SidebarBox()); %> 

Then have a view with just the code for your sidebar box.

FerretallicA
To make life a bit easier (assuming you're using the standard tooling) remember to generate your SidebarBox view as a strongly-typed Partial View of type List.
FerretallicA