ASP.NET MVC controller parameter optional (i.e Index(int? id))

I have the following scenario: my website displays articles (inputted by an admin. like a blog).

So to view an article, the user is referred to Home/Articles/{article ID}.

However, the user selects which article to view from within the Articles.aspx view itself, using a jsTree list.

So I what I need to do is to be able to differentiate between two cases: the user is accessing a specific article, or he is simply trying to access the “main” articles page. I tried setting the “Articles” controller parameter as optional (int? id), but then I am having problems “using” the id value inside the controller.

What is the optimal manner to handle this scenario? Perhaps I simply need a better logic for checking whether or not an id parameter was supplied in the “url”?

I am trying to avoid using two views/controllers, simply out of code-duplication reasons.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Use separate actions, like:

public ActionResult Articles() ...
public ActionResult Article(int id) ...

Alternatively move it to an Articles controller (urls using the default route will be: Articles and Articles/Detail/{id}):
public class ArticlesController : Controller
{
    public ActionResult Index() ...
    public ActionResult Detail(int id) ...
}

If you still must use it like you posted, try one of these:
public ActionResult Articles(int id = 0)
{
     if(id == 0) {
         return View(GetArticlesSummaries());
     }
     return View("Article", GetArticle(id));
}
public ActionResult Articles(int? id)
{
     if(id == null) {
         return View(GetArticlesSummaries());
     }
     return View("Article", GetArticle(id.Value));
}

Method 2

First of all, I agree with @Steve :). But if you really want to use

int? id

you can just check in your controller method if the id is set using a simple
if(id == null)

and if so, load all articles from your DB (or something alike) and pass these to your view (either directly, or by using a view model). If the id is set you just load the article having that id from your DB and send that to the view (possibly in a list as well if you dont use view models)?

Than in your view just load all articles in the list with articles supplied to the view. Which contains either all or just one.

Complete dummy code

public ActionResult showArticles(int? id){
   List<Articles> aList;
   if(id == null){
       aList = repository.GetAllArticles().ToList();
   }else{
      aList = new List<Articles>(); 
      aList.add(repository.GetArticleByID(id));
   }

   return View(aList);
}

Your View has something like:
<% Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
Inherits="System.Web.Mvc.ViewPage<List<Articles>>"%>

foreach(Articles a in Model)
    //display article

And you call it using either of the next two options:
html.ActionLink("one article","showArticles","Articles",new {id = aID},null}

html.ActionLink("all articles","showArticles","Articles"}

Method 3

Define a default value for the Id that you know indicated no value was supplied – usually 0.

public ActionResult Articles([DefaultValue(0)]int Id)
{
  if (Id == 0)
    // show all
  else
    // show selected
..

Method 4

The easiest solution is to have two different actions and views but name the actions the same.

public ViewResult Articles()
{
   //get main page view model
   return View("MainPage", model);
}

public ViewResult Articles(int id)
{
   // get article view model
   return View(model);
}

Method 5

this to me sounds like two separate pages and should be treated as such. You have the “Main” view page and the “articles” page.

I would split it into two actions. They should not be much dupliation at all really, both should be doing a call to get the same ModelView and the article will simply get the a extension to that!

Method 6

I don’t know if you tried this, but instead if you are typing the value directly into the URL, then, instead of passing it like this:

controller/action/idValue

try passing it like this:

controller/action?id=value

Method 7

Make the parameter required then set a default value in the routing that is a value that isn’t a valid index and indicates to your action that it should render the main page.

Method 8

Well, this is the combined solution I am using:

Using same controller, with DefaultValue:

public ActionResult Articles([DefaultValue(0)]int id)

If DefaultValue was used, I refer to “MainArticles” view. If an article ID was provided – I refer to the “Articles” view with the appropriate article passed inside the ViewModel.

In both cases the ViewModel is populated with the data both views need (complete article and category lists).

Thanks everyone for your help!


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x