代码之家  ›  专栏  ›  技术社区  ›  Brett Bim

使用Html.ActionLink传递文本框值

  •  5
  • Brett Bim  · 技术社区  · 16 年前

        <% foreach (var item in Model) { %>
    
        <tr>
            <td>
                <%= Html.Encode(item.Id) %>
            </td>
            <td>
                <%= Html.Encode(item.Description) %>
            </td>
            <td>
                <%= Html.Encode(String.Format("${0:F}", item.Cost)) %>
            </td>
            <td>
                <%= Html.TextBox(String.Format("quantity{0}", item.Id), "0") %>
            </td>
            <td>
                <%= Html.ActionLink("Add", "Add", new { id = item.Id, quantity="I want the quantity here?" })%>
            </td>
        </tr>
    
    2 回复  |  直到 16 年前
        1
  •  2
  •   Darrick Herwehe    8 年前

    我想你想要的是:

    <%= Html.TextBox(String.Format("item[{0}].quantity", item.Id), "0") %>
    

    请查看以下Scott Hanselman的博客文章,以了解更多有关这方面的详细信息:

    ASP.NET Wire Format for Model Binding to Arrays, Lists, Collections, Dictionaries

    Editing a variable-length list of items in ASP.NET MVC

        2
  •  3
  •   Eilon    16 年前

    在HTML中没有这样做的方法,因此在ASP.NET MVC中没有这样做的方法。

    有两种可能的解决方案可供选择:

    1. 使用JavaScript,当用户编辑文本框时,您可以动态更改锚定标记的值,以包含他们键入的内容。您不能对此使用ASP.NET路由,因为它在服务器上运行,并且您需要客户端代码。

    2. 执行表单提交,而不是链接。这是HTML中推荐的方法。当用户提交数据时,它应该是一个表单。将所有内容包装在表单标签中,并将文本框和按钮放在其中。将表单的操作设置为要将其发布到的URL。