2

ユーザーがコントローラーからテーブルに導入した値にアクセスしようとしています。

このテーブルはモデルの一部ではなく、ビューのソース コードは次のようなものです。

<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) を取得するにはどうすればよいですか?

テーブルを取得しないため、フォームコレクションの受信は機能しません...

ありがとうございました。

4

2 に答える 2

0

テーブルがタグFormCollection内にない限り、を使用すると機能するはずです<form>


Lazarus のコメントに加えて、これを試すことができますが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);"/>

Action メソッドで、メソッドを次のようにすることができます。

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

これらの値にseriales接続されます。

于 2011-01-05T15:56:32.800 に答える
0

最初のオプション: FormCollection を使用することは、動的データにアクセスする最も簡単な方法です。それらの値を取得できないのはおかしいですが、次のことを確認できますか?

  1. テーブルは要素の中にありますか?
  2. 入力要素に name 属性を追加できますか? フォーム項目は ID ではなく名前でバインドされることに注意してください。

2 番目のオプション: 2 番目のオプションは、モデルにコレクションを追加し、それに応じてすべてに名前を付けることです。すなわち

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

ビューでは、次の名前を使用します。

<input name="MyTableItems[]" value="" />
于 2011-01-05T16:05:22.310 に答える