1

I have this model:

 public class ReservationViewModel
 {        
  public Flight InboundFlight { get; set; }
  public Flight OutboundFlight { get; set; }
 }

 //Flight
 public class Flight
 {
  public List<ISeat> Seats { get; set; }
 }

 //Seats
  public interface ISeat 
  {
    ECabin Cabin { get; }
    int NumberOfSeatsAvailable { get; }
    int SeatsChosen { get; set; }
    string PropertyName { get; }
 }

My HTML consist of the folliwing:

<select id="OutboundFlight__0__Seats_SeatsChosen" name="OutboundFlight.[0].Seats.SeatsChosen" class="valid"><option...
<select id="OutboundFlight__0__Seats_SeatsChosen" name="OutboundFlight.[1].Seats.SeatsChosen" class="valid"><option...
<select id="OutboundFlight__0__Seats_SeatsChosen" name="OutboundFlight.[2].Seats.SeatsChosen" class="valid"><option...

My Action:

    [HttpPost]
    public ActionResult Index(ReservationViewModel model, FormCollection form)
    {            
        return View();
    }

Upon submit I try to bind back to the model but the Seats of each flight returns null... Help will be appreciated

2
  • You controller action(s) would be useful, too Commented May 2, 2012 at 16:30
  • I don't think there is enough information here to answer the question. Commented May 2, 2012 at 16:34

1 Answer 1

1

The HTML being generated is incorrect to get it to bind to a list - the field name has to match what what accessing the property from c# would look like:

This should work:

name="OutboundFlight.Seats[0].SeatsChosen"
Sign up to request clarification or add additional context in comments.

2 Comments

Now I get "Cannot create an instance of an interface" ..how to I solve this one?
Don't bind to an interface. The default modelbinder doesn't know how to create an ISeat. The easy way is to just change it to a concrete class, the harder way is to create a custom model binder.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.