簡體   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