使用Html.ActionLink传递文本框值

时间:2022-12-02 11:51:52

I have a table that lists products as well as displays a quantity text box and an Html.ActionLink. Each quantity textbox has a unique id derived from the product id. I think this should be simple but I can't seem to figure out how to get the value in the associated textbox passed to my controller when the user clicks on the link. My code is below and any help is appreciated.

我有一个列出产品的表,以及显示数量文本框和Html.ActionLink。每个数量文本框都具有从产品ID派生的唯一ID。我认为这应该很简单,但我似乎无法弄清楚当用户点击链接时如何获取传递给我的控制器的相关文本框中的值。我的代码如下,任何帮助表示赞赏。

    <% 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 个解决方案

#1


2  

I think what you want is this:

我想你想要的是这个:

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

Have a look at the following Scott Hanselman blog post for more details about this:

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

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

用于模型绑定到数组,列表,集合,词典的ASP.NET Wire Format

See also this blog post from Steve Sanderson. It will allow you to edit single items:

另见Steve Sanderson撰写的这篇博客文章。它允许您编辑单个项目:

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

在ASP.NET MVC中编辑可变长度的项列表

#2


3  

There is no way to do this in HTML, thus there isn't a way to do this in ASP.NET MVC.

没有办法在HTML中执行此操作,因此在ASP.NET MVC中无法执行此操作。

There are two possible solutions to this that you can choose from:

您可以选择以下两种可能的解决方案:

  1. Use JavaScript such that when the user edits the textbox you dynamically change the value of the anchor tag to include what they typed in. You can't use ASP.NET routing for this because that runs on the server and you need client side code.

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

  2. Do a form submit instead of a link. This is the recommended way in HTML. When the user is submitting data, it should be in a form. Wrap everything in a form tag and place the textbox and a button in there. Set the form's action to be the URL that you want it to post to.

    表单提交而不是链接。这是HTML中推荐的方式。当用户提交数据时,它应该是一个表单。将所有内容包装在表单标签中,然后将文本框和按钮放在那里。将表单的操作设置为您希望其发布到的URL。

#1


2  

I think what you want is this:

我想你想要的是这个:

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

Have a look at the following Scott Hanselman blog post for more details about this:

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

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

用于模型绑定到数组,列表,集合,词典的ASP.NET Wire Format

See also this blog post from Steve Sanderson. It will allow you to edit single items:

另见Steve Sanderson撰写的这篇博客文章。它允许您编辑单个项目:

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

在ASP.NET MVC中编辑可变长度的项列表

#2


3  

There is no way to do this in HTML, thus there isn't a way to do this in ASP.NET MVC.

没有办法在HTML中执行此操作,因此在ASP.NET MVC中无法执行此操作。

There are two possible solutions to this that you can choose from:

您可以选择以下两种可能的解决方案:

  1. Use JavaScript such that when the user edits the textbox you dynamically change the value of the anchor tag to include what they typed in. You can't use ASP.NET routing for this because that runs on the server and you need client side code.

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

  2. Do a form submit instead of a link. This is the recommended way in HTML. When the user is submitting data, it should be in a form. Wrap everything in a form tag and place the textbox and a button in there. Set the form's action to be the URL that you want it to post to.

    表单提交而不是链接。这是HTML中推荐的方式。当用户提交数据时,它应该是一个表单。将所有内容包装在表单标签中,然后将文本框和按钮放在那里。将表单的操作设置为您希望其发布到的URL。