Omer K Omer K - 2 months ago 8
C# Question

What is the special meaning of id in MVC

As you know when we want to go details of an item it is achieved by

@Html.ActionLink("Details", "Details", new { id=item.Id })


http://localhost:xxxx/Item/Details/1

What should we call this "1" parameter? It is not a querystring parameter but what is it? Does mvc interpret this parameter as a querystring parameter?

And another thing is, when I set the link as below:

@Html.ActionLink("Edit", "Edit", new { itemIdNo=item.Id })


mvc creates this link as:

http://localhost:xxxx/Item/Details?itemIdNo=1

As you see, they are quite different. Does "id" parameter have special meaning in MVC?

What should we call this "1" parameter? It is not a querystring parameter but what is it?

Does mvc interpret this parameter as a querystring parameter?

Answer

In your RouteConfig.cs, it adjusted as "id":

routes.MapRoute(
               name: "Default",
               url: "{controller}/{action}/{id}",
               defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

If you want to change it, you can change {id} parameter as {itemIdNo}. And also idparameter in "defaults:" line as itemIdNo.

Comments