1

意見

@model IEnumerable<MyMixtapezServerCodeHomePage.Models.album>

@for(int i=0;i<userchoiceIndex;i++)
{
    <div class="editor-label">
        @Html.LabelFor(model => model.artist)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.artist)
        @Html.ValidationMessageFor(model => model.artist)
    </div>
}

コントローラ

[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(IEnumerable<album> album)
{
}

出来ますか?データベースに複数の値をより速く簡単に作成したいと考えています。

4

3 に答える 3

2

可能ですが、そうではありません。

まず、次のように、アルバムを保持するモデルを作成します。

public class AlbumsModel
{
    public List<Album> Albums { get; set; }
}

次に、ビューで次の操作を行います。ループの使用に注意してくださいfor。これはname、アイテムの属性が同期され、モデル バインディングが投稿時にコレクションを簡単に解決できるようにするためです。

@model AlbumsModel

@for(int i=0; i<Model.Albums.Count; i++)
{
    <div class="editor-label">
        @Html.LabelFor(m=> m.Albums[i].artist)
   </div>
   <div class="editor-field">
       @Html.EditorFor(m=> m.Albums[i].artist)
       @Html.ValidationMessageFor(m => m.Albums[i].artist)
   </div>
}

次に、Postコントローラーのアクションを次のようにします。

[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(AlbumsModel model)
{
    foreach(Album album in model.Albums)
    {
        //do your save here
    }
    //redirect or return a view here
}
于 2012-12-04T17:27:54.983 に答える
1

フォーム ビューで次のようなことを試してください。コレクションのインデックスを設定する必要があります。

@model IEnumerable<MyMixtapezServerCodeHomePage.Models.album>

@for (int i=0; i<userchoiceIndex; i++)
{
    <div class="editor-label">
        @Html.LabelFor(model => model[i].artist)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model[i].artist)
        @Html.ValidationMessageFor(model => model[i].artist)
    </div>
}

コントローラーで次のようにします。

[HttpPost]
public ActionResult Create(IEnumerable<album> album)
{
    if (ModelState.IsValid) 
    {
       // persist and redirect... whatever
    }
    return View(album);
}

この記事をご覧ください: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

于 2012-12-04T17:28:19.190 に答える
0

すべての回答に感謝します。この情報は、私にとって役立つ単一のポイントになりました。

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

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

@{var item = @Model.ToList();}
@for (int count = 0; count < @Model.Count(); count++){
    <td>
        <div class="editor-label">
            @Html.LabelFor(model => item[count].name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => item[count].name)
            @Html.ValidationMessageFor(model => item[count].name)
        </div>
</td>
}

コントローラ

[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult FeatureSystem(IEnumerable<album> albums)
于 2013-02-05T14:52:47.800 に答える