0

My controller action is here:

[HttpPost]
public ActionResult Index(List<Widget> widgets, int index)
{
    widgets.RemoveAt(index);
    return View(widgets);
}

My post to this action looks like this:

[0].Name    a
[1].Name    b
[2].Name    c
index   1

When I debug, I see that there is only one item in my list. I can correct the issue by changing the parameter name from index to _index.

I couldn't find anything where index was a reserved word. Any thoughts as to why this happens?

4

1 に答える 1

1

Mvc モデル バインディングは、例のようにコレクション内のアイテムに連続するインデックスをバインドすることで、リストを操作できます。

ただし、指定されたキーを使用してバインドする mvc を取得できます。これは、 という名前の同じ特異性を持つフォーム フィールドを使用して行われindexます。

例えば

 A.index = "aaa"
 A[aaa].name = "hello"
 A.index = "bbb"
 A[bbb].name = "world"

Phil haack は、この http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspxに関する優れたチュートリアルを提供します。

Mvc はこの時点で混乱し、このタイプのバインディングを呼び出そうとする可能性があります

于 2012-11-09T16:36:27.013 に答える