c# - 如何将 TextBox 值传递给 MVC razor 中的 URL.Action?

标签 c# asp.net-mvc razor

我有一个 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/

相关文章:

c# - 在 C# 中使用 WMI 读取事件查看器内容时如何获取与文化无关的类型信息

c# - 如何将 .NET 标准库更改为 .NET 框架库?

asp.net - 使用 CLSID..Error 检索组件的 COM 类工厂

ASP.NET 图表控件不再适用于 .NET 4

c# - 使用 Ajax 在 ASP.Net MVC3 Razor 中呈现局部 View

c# - 向已序列化但不需要序列化的对象添加方法

c# - 正确播放Particle System组件?

asp.net-mvc - 从 MVC Controller 调用 Web API 来构建和传递模型以查看

asp.net-mvc - 在属性值中转义 @

c# - Razor 智能感知错误 : Feature 'extension method' cannot be used because it is not part of the ISO-2 C# language specification