0

I have made table with Actionlink. Actionlink open new View and as parametr to Action I want to send @item - full object. How can I make it?

Model:

public class MyClass
    {
        //I have properties here.
    }

Controller:

public class MyController : Controller
{
    public ActionResult ActionName(MyClass tm)
        {
            //have a breakpoint here
            return View();
        }
}

View:

<table>
    <tr>
        <th> Column title one </th>
        <th> Column title two </th>
        <th> Column title three </th>
    </tr>

@foreach (var item in @Model)
    {
        <tr>
            <td> @item.PropertyOne </td>
            <td> @item.PropertyTwo </td>
            <td> @item.PropertyThree </td>

            @if (ViewData["smth"] == "true")
                {
                    <td> @Html.ActionLink("Edit", "ActionName", "My", new { tm = @item }, null) </td>
                }
        </tr>  
    }
</table>

I need to say, that everything works fine, only after click to ActionLink, in MyClass tm is saved "null". Not the @item. How can I send it to Action if I don't want write "param1 = @item.propertyOne, param2 = @item.propertyTwo" etc.?

Thank you.

Ademar
  • 1,418
  • 1
  • 17
  • 27

1 Answers1

1

It's not really possible using a hyperlink (ActionLink). Anything you put as additional route values will mostly (except for areas) be translated into querystring key value pairs. You have lots of options though.

Because item is part of your model and therefore constructed in your action method, you could:

  • Cache the list of items in the action method (maybe via MemoryCache, TempData or maybe something like Redis), for use in another action method.

  • Store the list of items in the session.

  • Use the same method of retrieval (i.e. the same service call) to get the same items in another action method.

Community
  • 1
  • 1
Adrian Thompson Phillips
  • 6,893
  • 6
  • 38
  • 69