0

これには使用できません。編集タグのないビューでのみ使用List<album>できます。1 つのビューが正常に機能していますが、それらの 1 つのリストを編集することはできません。

@model IEnumerable<MyMixtapezServerCodeHomePage.Models.album>
@using (Html.BeginForm("FeatureSystem", "album", FormMethod.Post))
{

<table>

    <tr>
        <th>
            @Html.DisplayNameFor(model => model.name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.feature)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.feature_order)
        </th>
        <th></th>
    </tr>

    @foreach (var item in @Model)
    {

        <tr>
            <td>

                <div class="editor-label">
                    @Html.LabelFor(model => item.name)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => item.name,"album")
                    @Html.ValidationMessageFor(model => item.name)
                </div>
            </td>
            <td>
                <div class="editor-label">
                    @Html.LabelFor(model => item.feature,"album")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => item.feature)
                    @Html.ValidationMessageFor(model => item.feature)
                </div>
            </td>
            <td>
                <div class="editor-label">
                    @Html.LabelFor(model => item.feature_order)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => item.feature_order,"album")
                    @Html.ValidationMessageFor(model => item.feature_order)
                </div>
            </td>
            <td></td>
        </tr>





    }
    <input type="submit" />
</table>}

コントローラ

        [HttpPost]
    public ActionResult FeatureSystem(IEnumerable<album> albums)
    {
        foreach (album album in albums)
        {
            if (ModelState.IsValid)
            {
                albumRepository.Update(album);
                albumRepository.SaveChanges();
            }
        }

        return RedirectToAction("Index");
    }

アルバムが null であると受け取りました。なぜですか? 1 つのリストを編集できますか? これだけ必要です。

4

2 に答える 2

0

I believe this has not changed since mvc3

So to have an editable collection you've got to use a for loop, not a foreach, or binding won't work.

@for(var i = 0; i < Model.Count; i++)
 {
    <tr>
        <td>

            <div class="editor-label">
                @Html.LabelFor(model => Model[i].name)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => Model[i].name,"album")
                @Html.ValidationMessageFor(model => Model[i].name)
            </div>
        </td>
        <td>

  ...
}

take a look at this, old, but I think not outdated.

于 2013-02-02T21:22:06.680 に答える