0
votes

I get the following error when I click the Edit link from the List view

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

Here is my code:

Summary.ascx
Routes
Env Controller, Edit Action methods
Env Controller, List Action method
EnvRepository and SqlEnvRepository

1
I have a feeling that it depends on which URL you're trying to open... - Snowbear

1 Answers

2
votes

Your auto-generated links say this:

<td><%= Html.ActionLink("Edit", "Edit", new { id= Model.EnvironmentID} )%></td>

but the controller code says this:

public ActionResult Edit(int envId)

MVC's model binding hooks the parameters in the action up by name, and the default route assumes the first parameter will be an int called id. Change the name of your Edit() parameter to id and it should work.

Alternatively, you could change the ActionLink parameters object to new { envId = Model.EnvironmentID } but that will cause your URLs to look like this:

http://localhost/Env/Edit?envId = 1

instead of this:

http://localhost/Env/Edit/1