Rida Iftikhar
Rida Iftikhar

Reputation: 186

Call a controller method with parameters from partial view mvc 5

I'm trying to call an action method Details, which takes id as a parameter, in my UsersController from partial view. The link in my partial view is like this

@Html.ActionLink("My Account", "Details", "Users", routeValues: null, htmlAttributes: new { id = User.Identity.GetUserId() })

And this is my ActionMethod

public ActionResult Details(long? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        User user = db.Users.Find(id);
        if (user == null)
        {
            return HttpNotFound();
        }
        return View(user);
    }

i.e. a very basic action method generated by scaffolding. Now the problem is that when I click on the link the parameter 'id' is never appended to the url. The url looks something like this

localhost:8888/Users/Details

When it should have the appended the id value at the end. Does anyone know where I'm going wrong with this?

Upvotes: 1

Views: 8107

Answers (1)

diiN__________
diiN__________

Reputation: 7656

Don't try to pass it as htmlAttribute. Pass it like this:

@Html.ActionLink("My Account", "Details", "Users", new { id = User.Identity.GetUserId() }, null)

Upvotes: 2

Related Questions