2012-11-15 88 views
0

如何綁定模型以在mvc 4中列出?將模型綁定到asp.net中的列表mvc 4

+0

下面是一個usfull文章
[綁定模型例如:http ://www.codeproject.com/Articles/159749/ASP-NET-MVC-Model-Binding-Part1](http://www.codeproject.com/Articles/159749/ASP-NET-MVC-Model-Binding- Part1) –

+0

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx我在phill hack文章中發現了它自己。 – Garry

回答

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/