Mazher
Mazher

Reputation: 91

Routing in Mvc4 with id Issue in view

Event Controller

  public class EventController : Controller {

  public ViewResult Browse(Int32 EventId) {
   return View();
    }
  } 

Browse-View

 @model IEnumerable<Events.WebUI.Models.EventModel>
 @foreach (var e in Model) {
 <div id="event-content">
 <img src="@e.LogoUrl" width="512px" title="event header" height="83px">
 </div>
 <div class="event-text">
  @e.EventName
 @Html.ActionLink(@e.EventName, "Browse", "Event", new { EventId = @e.EventId })
 </div>

Here i want to use action method so that it should route to a action link with id as localhost/Event/Browse/1 and on hover it should show the link

Please Help me to resolve this issue, i also routed the path in route.config

Upvotes: 0

Views: 110

Answers (2)

Zafar
Zafar

Reputation: 3434

You have two options here:

1. General settings that works for all similar cases.

routes.MapRoute("Events", "{controller}/{action}/{eventId}", new {
            controller = "Home",
            action = "Index",
            eventId = UrlParameter.Optional
        });

2. Specific setting that works only for this case.

routes.MapRoute("EventsSpecific", "Event/Browse/{eventId}",
                            new {
                                controller = "Event",
                                action = "Index",
                                eventId = UrlParameter.Optional
                            });

You need to apply this in RouteCollection in RouteConfig.cs file inside the App_Start. But I've never tried route.config before but I'm sure they shouldn't make a big difference.

Last thing. Use Camel Case for parameter names such as eventId, instead of EventId. This is just a best practice.

Hope this helps.

Upvotes: 1

chamara
chamara

Reputation: 12711

@Html.ActionLink(@e.EventName, "Browse", "Event", new { EventId = @e.EventId },null)

URL will be like - localhost/Event/Browse?EventId=1

@Html.ActionLink(@e.EventName, "Browse", new { EventId = @e.EventId })

URL will be like - localhost/Event/Browse/1

Both methods should work for you.

Upvotes: 0

Related Questions