Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ActionLink将输入值从不同的表单元格传递到控制器方法_C#_Asp.net Mvc_Actionlink - Fatal编程技术网

C# ActionLink将输入值从不同的表单元格传递到控制器方法

C# ActionLink将输入值从不同的表单元格传递到控制器方法,c#,asp.net-mvc,actionlink,C#,Asp.net Mvc,Actionlink,我在MVC工作。 我有一张有一排的桌子。一个单元格包含一个“订单”按钮,另一个单元格包含一个数字输入,用户可以在其中指定他想要购买的商品的数量?如何从输入单元格传递ActionLink中的数量值?这里的问题不是查找文本值,而是查找特定行的文本值 表格单元格 <td><input id="itemQuantity" type="number" value="1" min="1" max="@item.QuantityInStock" size="10" clas

我在MVC工作。 我有一张有一排的桌子。一个单元格包含一个“订单”按钮,另一个单元格包含一个数字输入,用户可以在其中指定他想要购买的商品的数量?如何从输入单元格传递ActionLink中的数量值?这里的问题不是查找文本值,而是查找特定行的文本值

表格单元格

        <td><input id="itemQuantity" type="number" value="1" min="1" max="@item.QuantityInStock" size="10" class="universalWidth"/></td>
        <td>@Html.ActionLink("Order", "OrderTuckShop", new { id = item.IngredientId, quanity = ???}, new { @class = "btn btn-success universalWidth" })</td>

您不能在
ActionLink()
方法中添加文本框的值,因为它的razor代码在发送到客户端之前在服务器上解析。为了响应客户端事件,您需要使用javascript/jquery

创建一个手动链接,将
IngredientId
添加为
data-
属性,并添加一个额外的类名用作选择器

<td>
    <a href="#" data-id="@item.IngredientId" class="order btn btn-success universalWidth">Order</a>
</td>
<input class="quantity" type="number" ... />
然后包含以下脚本以读取输入值并进行重定向

var baseUrl = '@Url.Action("OrderTuckShop")';
$('.order).click(function() {
    var id = $(this).data('id');
    var quantity = $(this).closest('tr').find('.quantity').val();
    location.href = baseUrl + '?id=' + id + '&quanity=' + quanity;
});

但是,该方法的名称表明,您可能应该发布帖子,而不是GET?

@S.Akbari,how?检查重复答案。@S.Akbari这与此不同,因为我的文本值应该与按钮单击时来自同一行。我的问题是在行中找到正确的输入。标记为重复的答案与表或单元格值没有任何关系Action link helper是在服务器上处理的,因此您无法获取文本框的值并相应地生成链接,因为页面本身不会在浏览器中呈现。解决方法是将onclick事件附加到ActionLink,然后在函数中可以找到文本框的值并生成另一个httpget调用。查看此帖子
var baseUrl = '@Url.Action("OrderTuckShop")';
$('.order).click(function() {
    var id = $(this).data('id');
    var quantity = $(this).closest('tr').find('.quantity').val();
    location.href = baseUrl + '?id=' + id + '&quanity=' + quanity;
});