Abhay Andhariya
Abhay Andhariya

Reputation: 2157

How to pass String in Querystring Parameter in @Html.ActionLink

I have written following code:

@Html.ActionLink("Edit", "EditPost", new { id = item.PostID })

It displays URL like .../Post/EditPost/32 but actually I want to display it like ...../Post/EditPost?ID=32

How is it possible in Razor View???

Upvotes: 0

Views: 2280

Answers (2)

Amit
Amit

Reputation: 15387

Change Action Edit parameter from int to string. Then it will display as you want

public ActionResult Edit(string ID)
{
     =======
     =======
}

@Html.ActionLink("Edit", "EditPost", new { ID = item.PostID },null)

Another solution remove id = UrlParameter.Optional from the Global.asax/Route Table

Upvotes: 0

James
James

Reputation: 82096

The URL construct is generally dictated by the route, if you want to force it to use the query string then just remove any notion of a parameter from it e.g.

routes.MapRoute(
    "EditPostRoute",
    "Post/EditPost",
    new { controller = "Edit", action = "EditPost" }
);

Your @Html.ActionLink code should generate .../Post/EditPost?ID=32.

Upvotes: 1

Related Questions