0

IList を持つビュー モデルがあります。

public class MyMaintenanceListViewModel
{
    public IList<MyMaintenance> MyMaintenanceList { get; set; }

    [Display(Name = "Network User Name:")]
    public string NetworkUserName { get; set; }

    [Display(Name = "Password:")]
    public string Password { get; set; }
}

モデルがビューモデルに設定されているビューがあります:

@model EMMS.ViewModels.MyMaintenanceListViewModel

@using (Html.BeginForm("SubmitMaintenance", "Maintenance"))
{
    <table id="searchtable" class="MyMaintenance">
        <tr>
            <th style="width: 50px; text-align: left;">Id</th>
            <th style="width: 200px; text-align: left;">Equipment Id</th>
            <th style="width: 100px; text-align: left;">Task Id</th>
            <th style="width: 150px; text-align: left;">Date Completed</th>
            <th style="width: 100px; text-align: left;">Elapsed Time</th>
            <th style="width: 200px; text-align: left;">Created</th>
            <th style="width: 50px;"></th>
        </tr>
    @for (int i = 0; i < Model.MyMaintenanceList.Count; i++)
    {
        var item = Model.MyMaintenanceList[i];
       <tr>
            <td>
                @Html.DisplayFor(modelItem => item.RowId)
                @Html.HiddenFor(modelItem => item.RowId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.EquipmentId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.TaskId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.DateCompleted)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ElapsedTimeMinutes)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CreateDate)
            </td>
        </tr>
    }
    </table>
}

私のコントローラーは次のようになります。

[HttpPost]
public ActionResult SubmitMaintenance(MyMaintenanceListViewModel myMaintenanceListViewModel)
{
    // do something with IList "MyMaintenanceList" in myMaintenanceListViewModel
}

ただし、上記のコントローラーの post メソッドをブレークポイントしてフォームを送信すると、ビューにアイテムがあるにもかかわらず、myMaintenanceListViewModel の MyMaintenanceList リストに count=0 と表示されます。このテーブルの項目をコントローラーの post メソッドに渡すにはどうすればよいですか?

コントローラーの MyMaintenanceList リストの項目を反復処理しようとしています。これが理にかなっていることを願っています。

ありがとう

4

2 に答える 2

1

MVC モデル バインディングは、入力要素の name 属性を使用して、フォーム データをモデルにバインドします。まず、for ループで項目変数を作成しないでください。次のようにデータをバインドする必要があります。

    <tr>
            <td>
                @Html.DisplayFor(modelItem => Model.MyMaintenanceList[i].RowId)
                @Html.HiddenFor(modelItem => Model.MyMaintenanceList[i].RowId)
            </td>
   </tr>

次に、サーバーにデータを送信する場合は、入力タイプの要素を使用する必要があります。したがって、RowId の横にあるサーバーにデータを送信する場合は、MyMaintenanceList の他のプロパティに Html.HiddenFor を使用する必要があります

お役に立てれば。

于 2013-08-13T22:07:09.020 に答える
0

単純な CRUD アプリ以外の場合、[HttpPost] メソッドのビュー モデルを受け入れるのは不適切です。ViewModel は表示用であり、投稿用ではありません。

代わりに、次を試してください。

[HttpPost]
public ActionResult SubmitMaintenance(IList<MyMaintenance> myMaintenanceList)
{
    //Validate and then send to database
}
于 2013-08-13T22:05:29.587 に答える