ASP.NET MVC2 - passing a parameter to the controller

This is for the project that I am doing at the university. We use ASP.NET MVC2 to create a mini-prototype website to show what we can do.

Now I have a controller called ItemController.cs , view Item/Index.aspx , model Item.cs and ViewModel ViewItems.cs . I am using ViewModel to transfer information to a view from a controller. My question is: how can I call the controller method with parameters? I am currently using

 Html.ActionLink("View Event Items", "Index", "Item") 

which points to the ItemController Index() method. Let's say I want it to accept the int parameter ( Index(int eventId) ). How do I write ActionLink to pass this parameter?

Also, if I have any errors in the way I think this works, feel free to mention them.

+4
source share
2 answers

You will need to use the routevalues โ€‹โ€‹object (or RouteValuesDictionary) to pass values โ€‹โ€‹to your action. In your example, it will look like this:

 Html.ActionLink("View Event Items", "Index", "Item", new { eventId = 1}, new {}) 

... where 2 is your event id. The second empty object (new {}) is for html attributes. Nate's answer is close, but I don't think there is an overload that takes the routevalues โ€‹โ€‹object as the second parameter.

+2
source

I think that

 Html.ActionLink("View Event Items", new { controller = "Item", action = "Index", id = 5 }) 

should deliver you in the immediate vicinity of the place where you want to go.

0
source

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


All Articles