Unable to bind parameter

I have default routing:

routes.MapRoute(
            "Shortie", // Route name
            "{controller}/{id}", // URL with parameters
            new { controller = "Ettan", action = "Index", id = "id" } // Parameter defaults
            );

routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Ettan", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

I have a controller: NewsController. It has one method, for example:

public ActionResult Index(int id)
{
  ...
}

If I look through / News / Index / 123, it works. / News / 123 works. However, / News / Index? Id = 123 no (he cannot find any method named "index", where id is resolved as null). Therefore, it seems to me that I do not have enough understanding about how the router and the model node work together.

The reason for the request is that I want to have a drop-down list with different news sources with the parameter "id". Therefore, I can choose one news source (for example, "sport", id = 123), and it should be redirected to my index method. But I can't get this to work.

+3
1

ASP.NET MVC . , , . ... , , .

, ( ), id Nullable<int> i.e. int?.

+1

Source: https://habr.com/ru/post/1768563/


All Articles