0

私は次のようなモデルを持っています:

public class Invoice
{
int Code{get;set;}
List<InvoiceItem> InvoiceItems{get;set;}
}

InvoiceItems をテーブルに入力し、Get Table Item を Controller の InvoiceItems として取得したい

  [HttpPost]
        public virtual ActionResult Create(Invoice model)
        {
           //Save Invoice
            return View();
        }

InvoiceItems に入力してコントローラーに送信するにはどうすればよいですか?

ここに画像の説明を入力

4

2 に答える 2

1

HTML の結果は次のようになります。

    <input type="text" name="Code" value=""/>
    ...
    <table>
        <thead>
            <tr>
                <th>#</th>
                <th>Name</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>1</td>
                <td><input type="hidden" name="InvoiceItems[0].Name" value="InvoiceItem1"/></td>
            </tr>
            <tr>
                <td>2</td>
                <td><input type="hidden" name="InvoiceItems[1].Name" value="InvoiceItem2"/></td>
            </tr>
            <tr>
                <td>3</td>
                <td><input type="hidden" name="InvoiceItems[2].Name" value="InvoiceItem3"/></td>
            </tr>
        </tbody>
    </table>
    <button type="submit">Submit</button>
</form>

このようにして、プレーンフォーム送信または ajax を使用できます

// assuming you have only one form. otherwise use more specific selector
var $form = $("form");
var data = $form.serializeArray();
var url = $form.attr("href") || document.URL;
$.post(url, data, yourCallbackFunction);

残りは ModelBinder によって行われます。InvoiceItems重要な部分は、インデックスを維持することです。それらは 0 から始まり、連続している必要があります。つまり、0、1、2、3 などです。いくつかのインデックスをスキップすると、modelbinder はインデックスが壊れているリストのバインドを終了します。

于 2013-05-27T14:51:14.927 に答える