Zamir
Zamir

Reputation: 441

using guid in ActionLink

I search a lot,but didn't find any solution. I have a product table and here ProductID is guid. And in index.cshtml I have a edit link

@Html.ActionLink("Edit","Edit","Admin",new{id=i.ProductID})

When I click link, url like it

http://localhost:5546/Admin/Edit?Length=5

And I get following error

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Guid' for method 'System.Web.Mvc.ActionResult Edit(System.Guid)' in 'RiotBooks.Controllers.AdminController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

Parameter name: parameters How to I solve it?

Upvotes: 0

Views: 1008

Answers (1)

Tim
Tim

Reputation: 15237

You're using the wrong overload of ActionLink.

The one you're calling there is this one:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    Object routeValues,
    Object htmlAttributes
)

but you really want to be calling this one:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    string controllerName,
    Object routeValues,
    Object htmlAttributes
)

So just change your call to

@Html.ActionLink("Edit","Edit","Admin",new{id=i.ProductID}, null)

and you'll be good.

For the record, the tip off to me was that ?Length=5 comes from the fact that the string "Admin" being passed into the routeValues parameter has only one property on it (Length) and the length of that string is 5.

Upvotes: 3

Related Questions