我有一个 ASP.Net MVC 网站,我想使用 URL 操作将文本框值从 View 传递到 Controller 。
下面是我的代码,
<table class="table table-striped table-bordered table-hover" id="products" width="100%">
<thead>
<tr>
<th>ProductId</th>
<th>Name</th>
<th>ShortName</th>
<th>ProductNamePrefix</th>
<th>Minimum Count</th>
<th>Add Product</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ProductId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.ShortName)
</td>
<td>
@Html.DisplayFor(modelItem => item.ProductNamePrefix)
</td>
<td>
@Html.TextBox("MinCount", item.MinimumCount, new {@class = "form-control" + " " + item.ProductId})
</td>
<td>
@if (item.IfExists == true)
{
<div class='isa_success'><i class='fa fa-check'></i></div>
}
@if (item.IfExists == false)
{
<a href="@Url.Action("AddProduct", "Home", new { ProductId = item.ProductId, Name = item.Name, ShortName = item.ShortName, ProductNamePrefix= item.ProductNamePrefix, MinimumCount= item.MinimumCount})"><div class='isa_info'><i class='fa fa-plus-circle'></i></div></a>
}
</td>
</tr>
}
</tbody>
</table>
我想将 url 操作方法中的文本框值传递给 Controller 。我正在根据文本框的 id 创建一个自定义类,但我无法使用 javascript 或 jQuery 检索它。
最佳答案
but I am not able to retrieve it using javascript or jQuery
为什么不呢?这是pretty simple .
在没有 JavaScript 的情况下,您无法仅使用链接来完成此操作。相反,您需要使用表格。因此,您必须将每个表格行包装在一个 form
元素中,并将您的链接替换为 input type="submit"
。 (当然,根据您的需要设置样式。)这会将包含的表单元素(包括文本框)的值发布到表单的操作。
这确实有点难看,因为你不能用 form
包裹 tr
,所以你必须 < em>嵌套你的结构。像这样:
<table>
<tr>
<td>
<form>
<table><!-- Your 1-row table goes here --></table>
</form>
</td>
</tr>
</table>
outer tr
是“表格”的每一行重复的内容,因此每一行实际上是一个包含嵌套表格的单元格,而嵌套表格本身只有一个排。正如我所说,有点丑陋。但它完成了工作。
关于c# - 如何将 TextBox 值传递给 MVC razor 中的 URL.Action?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35001019/