Thiago
Thiago

Reputation: 31

asp net mvc 3 post IEnumerable Model

I found on post here, and it is exactly what I want: post IEnumerable to my controller.

here's the other post:

Foreach on IEnumerable property and CheckBoxFor in ASP.Net MVC

I tried something very close to this:

http://www.vbforums.com/showthread.php?t=652925

but when I submit the form, the model is null on controller.

I found one solution on ASP NET MVC 2, using Html.BeginCollectionItem() , but it looks like they removed this helper on version 3. ( http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/).

Upvotes: 3

Views: 2035

Answers (2)

Daryl Teo
Daryl Teo

Reputation: 5495

Just use string[]

<form action="url"> 
   <input type="text" name="data" />
   <input type="text" name="data" />
   <input type="text" name="data" />
   <input type="text" name="data" />
   <input type="text" name="data" />
</form>


public ActionResult Url(string[] data){

}

This personally works for me on my project. Although I haven't tried it on complex types, so give that a shot)

Upvotes: 1

Van Thoai Nguyen
Van Thoai Nguyen

Reputation: 1036

I think we can't use Interface or Abstract class as the parameter of Action Methods because the MVC Engine will have no clue to make the instance of your model. However, I think a custom model binder could do this. Check how to create a custom binder for IEnumerable which will be similar to this one:

http://davidhayden.com/blog/dave/archive/2008/09/08/CustomModelBinderMoreUIValidationASPNETMVC.aspx

Upvotes: 0

Related Questions