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

后端 未结 8 1158
日久生厌
日久生厌 2020-12-19 01:53

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/{articl

8条回答
  •  别那么骄傲
    2020-12-19 02:12

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

提交回复
热议问题