0

モデルを mvc 4 のリストにバインドするにはどうすればよいですか?

4

1 に答える 1

1

MVC プロジェクトを作成し、すべてをデフォルトのままにして、次の (非常に単純な) コードを使用してモデル フォルダーに製品を追加します。

namespace BlogPost.Models
{
public class Product
{
    public string Name { get; set; }
}
}   

次に、次のコードを使用して ProductsController (Controllers フォルダー内) を追加します。

using System.Collections.Generic;
using System.Web.Mvc;
using BlogPost.Models;

namespace BlogPost.Controllers
{
public class ProductsController : Controller
{
    public ActionResult Add()
    {
        return this.View();
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Add(IList<Product> products)
    {
        return this.View();
    }
 }
}

ソース: http://kristofmattei.be/2009/10/19/asp-net-mvc-model-binding-to-a-list/

于 2013-03-14T19:43:28.363 に答える