0

エディター テンプレートを使用して動的フィールドを表示する方法を示すこの記事を読みましたhttp://coding-in.net/asp-net-mvc-3-how-to-use-editortemplates/

[フィールドの追加] ボタンをクリックして新しいフィールドを作成するにはどうすればよいですか?

私はこれをググっていくつかの例を見ましたが、単純なプロセスであるべきものには複雑すぎるようです. PHP では[]、フィールド名の末尾に追加するだけで、ループを実行できます...

MVC は新しい技術であるため、MVC でこれを行うためのさらに簡単な方法があるはずですよね?

4

1 に答える 1

4

動的フォーム フィールドは、次のように簡単に実現できます。

モデル:

public class MyViewModel
{
    public string Name { get; set; }
    public List<string> EmailAddresses { get; set; }
}

意見:

@model MyViewModel

@Html.TextboxFor(m => m.Name)

@* you can add as many of these as you like, or add them via javascript *@
<input type="text" name="EmailAddresses" />
<input type="text" name="EmailAddresses" />
<input type="text" name="EmailAddresses" />
@* you can also use razor syntax *@
@Html.Textbox("EmailAddresses")

コントローラ:

[HttpPost]
public ActionResult MyAction(MyViewModel model)
{
    // note that model.EmailAddresses contains your list of email addresses
    foreach (string email in model.EmailAddresses)
        email = email; // do whatever you want with this...

    return View();
}
于 2015-08-25T12:06:05.573 に答える