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 回答