0

辞書を含むモデルをhttppost経由で送信しようとしていますが、その辞書は常にnullです...これが私のモデルです:

public class RoomListModel
    {
        public Dictionary<PersonModel, List<LocationModel>> list { get; set; }
        public String mess { get; set; }
        public RoomListModel(Dictionary<PersonModel, List<LocationModel>> list, String mess)
        {
            this.mess = mess;
            this.list = list;
        }

        public RoomListModel()
        {
        }
    }

最初のビューでは、その種のモデルが正しく入力されて表示されます (私はいくつかのテストを行いました)。次に、ビューからコントローラーにいっぱいに送信しようとします。これが私の見解です:

@for (int i = 0; i < Model.list.Count(); i++) {
                <tr>
                    <td>
                        @Html.EditorFor(m => m.list.ElementAt(i).Key.isSelected)
                    </td>
                    <td>
                        @Html.DisplayFor(m => m.list.ElementAt(i).Key.login)
                        @Html.HiddenFor(m => m.list.ElementAt(i).Key.login)
                    </td>
                    <td>
                        @Html.DisplayFor(m => m.list.ElementAt(i).Key.role)
                        @Html.HiddenFor(m => m.list.ElementAt(i).Key.role)
                    </td>
                    <td>
                        @for (int j = 0; j < Model.list.ElementAt(i).Value.Count(); j++)
                        {
                            @Html.EditorFor( m => m.list.ElementAt(i).Value.ElementAt(j).isSelected )
                            @Html.DisplayFor( m => m.list.ElementAt(i).Value.ElementAt(j).id )
                            @Html.HiddenFor( m => m.list.ElementAt(i).Value.ElementAt(j).id )
                            @Html.HiddenFor( m => m.list.ElementAt(i).Value.ElementAt(j).name)<br />
                        }
                    </td>
                </tr>
            }

しかし、そのフォームが送信されると、 nullReferenceException が発生しました:

[HttpPost]
        public ActionResult CreateInventory(RoomListModel mod)
        {
            int nbPer = 0;
            foreach (var per in mod.list)
            {
                if (per.Key.isSelected)
                {
                    nbPer++;
                }
            }
            if (nbPer == 0)
            { ...

mod.list で。

私のコードの何が問題なのか教えてください。Dictionnary オブジェクトが原因ですか? ありがとう !

4

0 に答える 0