2011-01-05 97 views
2

我試圖訪問用戶從我的控制器表中引入的值。從HTML獲取值到控制器

這表不是模型的一部分,並認爲源代碼是一樣的東西:

<table id="tableSeriales" summary="Seriales" class="servicesT" cellspacing="0" style="width: 100%"> 
    <tr> 
     <td class="servHd">Seriales</td> 
    </tr> 
    <tr id="t0"> 
     <td class="servBodL"> 
      <input id="0" type="text" value="1234" onkeypress = "return handleKeyPress(event, this.id);"/> 
      <input id="1" type="text" value="578" onkeypress = "return handleKeyPress(event, this.id);"/> 
      . 
      . 
      . 
     </td> 
    </tr> 
</table> 

我怎樣才能從控制器的值(1234,578)?

接聽的FormCollection,因爲它沒有得到該表不工作...

謝謝。

+2

的形式收集應接收輸入控制值,則可能是使用號碼,ID的問題。嘗試使用id =「control1」等。 – Lazarus 2011-01-05 15:53:52

回答

0

使用FormCollection應該工作,除非你的表是不是<form>標籤


拉撒路的評論頂部的裏面,你可以試試這個,但你必須設置name屬性每個:

<input id="seriales[0]" name="seriales[0]" type="text" value="1234" onkeypress="return handleKeyPress(event, this.id);"/> 
<input id="seriales[1]" name="seriales[1]" type="text" value="578" onkeypress="return handleKeyPress(event, this.id);"/> 

現在您的操作方法可以讓你的方法是這樣的:

[HttpPost] 
public ActionResult MyMethod(IList<int> seriales) 
{ 
    // seriales.Count() == 2 
    // seriales[0] == 1234 
    // seriales[1] == 578 
    return View(); 
} 

seriales將連線到這些值。

0

第一個選項: 使用FormCollection是訪問動態數據的最簡單方法。奇怪的是,你無法從中獲得這些值,你可以檢查以下內容嗎?

  1. 是 元素中的表?
  2. 您可以將名稱屬性 添加到輸入元素嗎?請注意, 表單項是由它們的名稱綁定的, 不是id。

第二個選項: 第二個選項是增加你的模型集合,並依此命名的一切。即

public class MyModel 
{ 
    ... 
    public IList<string> MyTableItems { get; set; } 
} 

,並在您的視圖使用以下名稱:

<input name="MyTableItems[]" value="" />