开发者

Passing Textbox Value using Html.ActionLink

开发者 https://www.devze.com 2022-12-13 20:17 出处:网络
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

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 be开发者_如何学Pythonlow and any help is appreciated.

    <% 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>


There is no way to do this in HTML, thus there isn't a way to do this in 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.

  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.


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:

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

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消