Search code examples
c#.netasp.net-mvcrazoractionlink

After clicking on an actionlink, how do I reference back to its linkText?


Say I have a table that begins like this:

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.ActionLink(item.Name, "Things", new { someId = item.Id})
        </td>

And it takes me to the view for that specific item. Once I am in that separate .cshtml file, how can I reference back to the original "item.Name" string that appeared in this ActionLink?

* SOLUTION EDIT *

The solution ended up looking like this.

The ActionLink:

@Html.ActionLink(item.Name, "Things", new { someId = item.Id, someName = item.Name})

The Action:

public ActionResult Things(Guid? someId, string someName)
...
ViewBag.currentName = someName;

The other View:

<h2>@ViewBag.currentName</h2>

Solution

  • I think you don't have to reference to first ActionLink argument. What might interest you is an object which is a Model for this view. When you click the link you will navigate to action where you pass the someId - id of current model object. Then probably you should find an object by its id in your storage. It will have .Name property that was used as ActionLink argument.

    However if you just need exactly the same string you've used as a first argument in ActionLink which can be any string you should duplicate it in route values. Your Things action should accept not only someId argument but also this string. After you should pass this string via Model or ViewBag/ViewDictionary to your view.