0

On my Index page I have the following link to the Details view:

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

My controller is expecting an int:

public ActionResult Details(int ClubId)
{
    var club = _service.GetClub(ClubId);
    var model = AutoMapper.Mapper.Map<ClubViewModel>(club);
    return View(model);
}

Im getting this every time though:

The parameters dictionary contains a null entry for parameter 'ClubId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Details(Int32)' in 'MyProject.Web.Controllers.ClubsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

I know this is something to do with routing however I have tried swapping UrlParameter.Optional to "" and making the ViewModel's ClubId nullable but the error remains.

If I rewire my controller to accept a Club object and pass in item from the Index view then everything is fine and the ClubId is populated in debug but I'm left with a stupidly large parameter list in the URL.

I don't really get what the problem is here?

4
  • 3
    have you tried using ClubId instead of just id? Commented Feb 9, 2015 at 16:37
  • For gods sake, thanks Commented Feb 9, 2015 at 16:40
  • There is a difference between /url/parameters and ?query=string&parameters. The former affects the routing, the latter does not. Commented Feb 9, 2015 at 16:40
  • would you mind marking mine as the answer? Thanks a lot :D Commented Feb 9, 2015 at 16:52

2 Answers 2

2

Your controller is expecting a parameter named ClubId but you're passing a parameter called id. They need to match.

Sign up to request clarification or add additional context in comments.

1 Comment

Sometimes I've experienced a weird issue with id parameter name not working at all, that is parameter was not populated when the action was called. So these should better match as ClubId
1

have you tried using ClubId instead of just id?

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.