【问题标题】:How to change list of inputs in a form to a list of links?如何将表单中的输入列表更改为链接列表?
【发布时间】:2012-08-09 20:26:30
【问题描述】:

如何使以下代码成为链接列表而不是按钮列表?

@foreach (var item in @Model)
{ 
    using (Html.BeginForm(new { action = "GetL", controller = "L" }))
    {
         <input name="fileLocation" type="submit" value="@item" />
    }
}

【问题讨论】:

  • 嗯,从&lt;input name="fileLocation" type="submit" value="@item" /&gt;&lt;a href='destinationURL'&gt;Text&lt;/a&gt;

标签: c# asp.net .net razor


【解决方案1】:
@foreach (var item in @Model)
{ 
      using (Html.BeginForm(new { action = "GetL", controller = "L" }))
      {
           <a name="fileLocation" href="@item">@item</a>
      }
}

【讨论】:

  • href="@item" 肯定是不正确的。考虑到 OP 的可能级别,最好提一下,他们需要为此字段编写一个合法的 url。
【解决方案2】:

你可以使用Html.ActionLink辅助方法来生成锚标签

@foreach (var item in @Model)
{ 
   using (Html.BeginForm(new { action = "GetL", controller = "L" }))
   {
       @Html.ActionLink(item,"yourAction","YourController")           
   }
}

如果你想将一些参数(例如:Id)传递给 Action 方法,你可以使用this override

@Html.ActionLink(item,"yourAction","YourController",
                                                     new { @id="someVal"} ,null)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 2022-01-11
    • 2013-09-17
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    相关资源
    最近更新 更多