I found on post here, and it is exactly what I want: post IEnumerable to my controller.
here's the other post:
开发者_StackOverflow社区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/).
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)
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
精彩评论