我可以从View发送到Controller完整对象(@item)吗?

时间:2013-09-04 11:34:41

标签: asp.net-mvc-4 razor

我用Actionlink制作了表格。 Actionlink打开新的View和参数作为Action我想发送@item - 完整对象。我该怎么做?

型号:

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

控制器:

public class MyController : Controller
{
    public ActionResult ActionName(MyClass tm)
        {
            //have a breakpoint here
            return 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>

我需要说,一切正常,只有点击动作链接后,在MyClass中,tm保存为“null”。不是@item。如果我不想写“param1 = @ item.propertyOne,param2 = @ item.propertyTwo”等,如何将其发送给Action?

谢谢。

1 个答案:

答案 0 :(得分:1)

使用超链接(ActionLink)实际上是不可能的。作为附加路径值放置的任何内容(主要区域除外)都将转换为查询字符串键值对。你有很多选择。

因为item是模型的一部分,因此在你的action方法中构造,你可以:

  • 缓存操作方法中的项目列表(可能通过MemoryCacheTempData或类似Redis),以便在其他操作方法中使用。

  • 在会话中存储项目列表。

  • 使用相同的检索方法(即相同的服务调用)来获取 另一种行动方法中的相同项目。

相关问题