0

I have an generic enumerable of type BookCover that I wan't to display to the user. They can only choose one book cover from the selection available.

 public class BookCover {
        public bool IsSelected { get; set; }
        public string CoverPathThumb { get; set; }
        public string SpinePathThumb { get; set; }
        public string BackPathThumb { get; set; }
    }

My action method is similar to

public ActionResult SelectCover(IEnumerable<BookCover> covers);

In my template I just enumerate and write out the desired HTML but the problem is I don't know how to get the data from the post back.

How do I name my <input> id's? Is there another reason IEnumerabme isn't populating when the post back occurs?

4 Answers4

2

@Vince: You can customize the ModelBinder. And in the ModelBinder, you can get data from HttpContext.Request.Form, after that you will build your new BookCover collection. Finallly you call

public ActionResult SelectCover(IEnumerable<BookCover> covers);

And remember registering it in Global.asax as:

ModelBinders.Binders[typeof(IEnumerable<BookCover>)] = new YourModelBinderName();

You can get references at here and here is discussion about it. Hope this help you!

Community
  • 1
  • 1
thangchung
  • 2,020
  • 2
  • 17
  • 28
0

You should add an ID you BookCover type, and then use this ID to identify the cover that the user selected. If you retrieve your covers from a database, just use this ID in your class.

Jan Aagaard
  • 10,940
  • 8
  • 45
  • 80
0

I think you can do something like this:

<% foreach(var item in covers) { %>
     <%: Html.EditorFor(x => item.IsSelected) %>
<% } %>
synepis
  • 1,292
  • 16
  • 28
0

The name of your inputs should be in the form:

covers[0].IsSelected
covers[0].CoverPathThumb
covers[0].SpinePathThumb 
covers[0].BackPathThumb 

E.g.

<input type="text" name="covers[0].CoverPathThumb" />

Increase 0 for each cover entry.

drew
  • 1,312
  • 8
  • 20