1

I'm in the process of building a simple website using MVC 4 and razor. I'm having trouble receiving an int from an url.action that is being filled on the fly. I have debugged and found that the id does have a value (the id happens to just be 1) but when it is passed it somehow becomes null.

The View portion:

@if (Model.Any())
{
  foreach (var e in Model)
  {
    <a href="@Url.Action("EventDetails", "Event", new{id = e.ID})">
      <div id="eventBox">
        <!-- stuff !-->
      </div>
    </a>
  }
}

And the controller:

public ActionResult EventDetails(int? eventID)
{
     if (eventID != null)
     {
         //stuff
     }
}

I really don't know what is happening behind the scenes to cause it to change to null and I need help. I'm using just the default routing provided by Visual Studio right now:

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

1 Answer 1

2

Your passing a route value named id

new { id = e.ID }

so change the method parameter name to match it

public ActionResult EventDetails(int? id)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.