【问题标题】:passing multiple parameters in @html.actionlink()在@html.actionlink() 中传递多个参数
【发布时间】:2014-02-19 19:45:36
【问题描述】:

我正在使用 actionlink 将参数“code”传递给一个项目的控制器,并在新视图上获取该项目的详细信息(实际上,它是调用 3 个局部视图的控制器)

我还需要传递参数“描述”,但这只是一个需要在新视图上显示的值,在控制器中没有用(我正在使用“代码”过滤数据库记录)。

@foreach (var item in Model.MyModel)
{
  <tr>
    <td>
      @Html.ActionLink(item.code, "Index", "ControlerName", new { pos = item.code.ToString(), desc = item.desc.ToString() }, null)
</td>

}

这个 actionlink 给我 url /ControlerName?pos=code&desc=desc

有没有办法发送“desc”参数,这样它就不会在 url 中看到,而只会在新视图中显示?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 razor actionlink


    【解决方案1】:

    您必须像传递参数一样传递参数,以便其他控制器和视图知道正在持久化的数据。

    另一种方法是将您的描述存储在您可以通过id 识别的“查找表”中,因此您只需传递id,这将是Controller'sViewModel's 的责任查一下。

    @Html.ActionLink(item.code, "Index", "ControllerName", new { id=1 }, null)
    

    控制器:

    public ActionResult Index(int id) {
        // Go to DB and get the description by id.
    }
    

    【讨论】:

    • 我的回复被标记为答案,但你的回复更好!
    【解决方案2】:

    不,Html.ActionLink 仅用于生成 url 并将其放入锚标记中。您应该在被击中时从您的 Index 操作中重新填充 desc 值。

    【讨论】:

      【解决方案3】:

      使用表单标签并将你想要的所有参数传递给actionresult并返回你想要的任何其他actionresult(它将携带你想要的url)。

      razor asks for ; when doing using(Html.BeginForm())

      【讨论】:

      • 我很想听听您可以通过什么其他方式传递参数而不将其包含在 url 中!既然您投了反对票,请您回答或发表评论以澄清原因吗?
      猜你喜欢
      • 2012-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-21
      • 1970-01-01
      • 2012-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多