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

49,220

Solution 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));
}

Solution 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"}

Solution 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
..

Solution 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);
}

Solution 5

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

Share:
49,220
Tom Teman
Author by

Tom Teman

Software Developer @ WatchDox SOreadytohelp

Updated on November 21, 2020

Comments

  • Tom Teman
    Tom Teman over 3 years

    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.

  • Ryan
    Ryan over 13 years
    Using the DefaultValue attribute seems like a very confusing way to solve this problem.
  • Tom Teman
    Tom Teman over 13 years
    hmmmm... perhaps I will use 2 views after all. I need the main introductory article page to be more "interesting" HTML wise.
  • Tom Teman
    Tom Teman over 13 years
    Ok, but when I try to call the main articles page using: <%= Html.ActionLink("Articles", "Articles", "Home")%> I get an error: The current request for action 'Articles' on controller type 'HomeController' is ambiguous between the following action methods: System.Web.Mvc.ActionResult Articles() on type dr_teman_MVC.Controllers.HomeController System.Web.Mvc.ActionResult Articles(Int32) on type dr_teman_MVC.Controllers.HomeController
  • Ryan
    Ryan over 13 years
    Does your (default) route specify a default id? I suppose that could be an issue. I would probably name one action Articles and the other Article.
  • Tom Teman
    Tom Teman over 13 years
    "Alternatively move it to an Articles controller (urls using the default route will be: Articles and Articles/Detail/{id}):" this is probably the best way to go about it. Thanks!