Garry
Garry

Reputation: 5074

bind a model to list in asp.net mvc 4

How we can bind a model to list in mvc 4 ?

Upvotes: 0

Views: 1877

Answers (1)

Pamma
Pamma

Reputation: 1455

Create a MVC Project, leave everything default and add Product in your Models folders with the following (very simple) code:

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

Then add a ProductsController (in the Controllers folder) with the following code:

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();
    }
 }
}

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

Upvotes: 1

Related Questions