繁体   English   中英

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

[英]How to pass TextBox value to URL.Action in MVC razor?

我有一个ASP.Net MVC网站,我想使用URL Action将文本框值从视图传递给控制器​​。

以下是我的代码,

<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 action方法中的文本框值传递给controller。 我正在基于文本框的id创建自定义类,但我无法使用javascript或jQuery检索它。

但我无法使用javascript或jQuery检索它

为什么不? 很简单

如果没有JavaScript,您只能通过链接来完成此操作。 相反,您需要使用表单。 因此,您必须将每个表行包装在form元素中,并使用input type="submit"替换您的链接。 (当然,根据您的需要对其进行设置。)这会将包含的表单元素(包括文本框)的值发布到表单的操作中。

这确实有点难看,因为你不能在tr周围包裹一个form ,所以你必须嵌套你的结构。 像这样的东西:

<table>
  <tr>
    <td>
      <form>
        <table><!-- Your 1-row table goes here --></table>
      </form>
    </td>
  </tr>
</table>

外部 tr是为“表”的每一行重复的内容,因此每行实际上是一个包含嵌套表的单元,该表本身只有一行。 正如我所说,有点难看。 但它完成了工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM