1

何かを操作するためだけに、いくつかのダミーデータをビューに渡します。

public ActionResult Index()
        {
            ViewBag.Message = "Create table";
            var model = new List<Auction>();
            model.Add(new Auction
            {
                Title = "First Title",
                Description = "First Description"
            });
            model.Add(new Auction
            {
                Title = "Second Title",
                Description = "Second Description"
            });
            model.Add(new Auction
            {
                Title = "Third Title",
                Description = "Third Description"
            });
            model.Add(new Auction
            {
                Title = "Fourht Title",
                Description = "Fourth Description"
            });

            return View(model);
        }

これを私のビューに表示します:

@model List<Ebuy.Website.Models.Auction>

@{
    ViewBag.Title = "Home Page";
}


@using (Html.BeginForm())
{
    <table border="1" >
        @for (var i = 0; i < Model.Count(); i++)
        {
            <tr>

                <td>
                    @Html.HiddenFor(x => x[i].Id)
                    @Html.DisplayFor(x => x[i].Title)
                </td>
                <td>
                    @Html.EditorFor(x => x[i].Description)
                </td>
            </tr>
        }
    </table>

    <button type="submit">Save</button>
}

そして今、送信されたデータを操作したいと思います:

 [HttpPost]
        public ActionResult Index(List<Auction> model)
        {
            var test = model;
            model[1].Title = "Test";
            return View(model);
        }

しかし、これをデバッグすると、Descriptionプロパティが送り返されていることがわかりますが、Titleプロパティには値がありません。上記のように、 で表示します@Html.DisplayFor(x => x[i].Title)。これはデフォルトの動作ですか、それとも何か間違っていますか?

4

1 に答える 1

6

@Html.DisplayFor入力フィールド(またはその問題のフォームフィールド)を出力しないため、フォームが投稿する値の一部ではありません。

タイトルをフォームの一部として投稿したい場合は、追加することができます

@Html.HiddenFor(x => x[i].Title)

于 2013-05-05T19:27:19.520 に答える