Taras Bilynskyi
Taras Bilynskyi

Reputation: 1504

Html.ActionLink magic

I have the following model:

public class AccountDetailsViewModel
{
   public Guid ID { get; set; }
   public string FirstName { get; set; }
   public string LastName { get; set; }
}

Then I have AccountController with followign action

public ActionResult Details(Guid? id)
{
    var model = new AccountDetailsViewModel
    {
        ID = 2,
        FirstName = "FirstName",
        LastName = "LastName"
    };
    return View(model)
}

And strongly type view for this action

@model AccountDetailsViewModel
...
@Html.ActionLink("My Page", "Details", "Account")
...
//display info about user

The idea is that when user access Account/Details url without id parameter his own info is displayed, and when id is present in the url info about user with that id is shown.

The problem is that when user access url with id (someone's other page), the link My Page(on the top of the view) points to the same page. ActionLink takes id of the currently displayed user, and this is a problem, as I want My Page link always point to Account/Details (to my page). This id for ActionLink is not taken from @Model as I thought first, it look like there is some other magic which i am not fammiliar with.

The same problem I have with other pages.

So what is going on and how to prevent this type of behaviour?

Upvotes: 3

Views: 205

Answers (1)

Brandon
Brandon

Reputation: 69983

ASP.NET MVC re-uses routing variables if you don't specify what they are. So if I'm on someone elses page that matches the routing, their ID is going to be used even if you didn't explicitly say to use it.

@Html.ActionLink("My Page", "Details", "Account", new { id = "" })

Just set it to blank to clear out the ID parameter.

Upvotes: 2

Related Questions