I have created a table in the database that has a System.Guid as it's primary key. The required ADO.Net Entity Framework model has been generated and the required stored procedures has been mapped.
I created a new controller and added the basic required code for Create and Edit for the data. However when the clicking on the link to edit the a particular record the following error is generated:
The parameters dictionary contains a null entry for parameter '[MyId]' of non-nullable type 'System.Guid' for method 'System.Web.Mvc.ActionResult Edit(System.Guid)' in '[MySite].[MyController].[SpecificController]'. To make a parameter optional its type should be either a reference type or a Nullable type.
Parameter name: parameters
The edit action is declared in the controller as follows:
public ActionResult Edit(Guid itemId)
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(MyItem itemToModify)
{
}
When adding a new record the new Guid is generated via a Stored Procedure and the list is displaying the correct Guid. The Url is also passing the correct Guid for retrieval.
I can't seem to capture the point at which this fails, but how would I go about passing a System.Guid as a parameter to the Controller?
Unless you've updated your routes, it is expecting (by default) the final parameter in a route to be named "id". That is, if you have a route like /specific/edit/5646-0767-..., it will map the guid into the route value dictionary with key "id" regardless of what the parameter on your method is named. I'd follow this convention and change the method definition to:
public ActionResult Edit(Guid id)
You can get around this, by explicitly specifying the name of the route parameter, but then you end up with a url that looks like: /specific/edit?itemid=5646-0767-...
Putting sample code below for anyone who may need it (I sure did)
public ActionResult Profile(Guid? id)
{
if (!id.HasValue)
{
return View(new ProfileRepository().GetUserProfile(User.Identity.Name));
}
return View(new ProfileRepository().GetUserProfile(id.Value));
}
Thank you for the anwer above which led me in the right direction
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With