MVC模型与列表绑定

时间:2012-05-02 16:27:16

标签: asp.net-mvc-3 model-binding

我有这个型号:

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

我的HTML包含以下内容:

<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...

我的行动:

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

提交后我尝试绑定回模型但每个航班的座位返回null ... 帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

生成的HTML不正确,无法将其绑定到列表 - 字段名称必须与从c#访问属性的内容相匹配:

这应该有效:

name="OutboundFlight.Seats[0].SeatsChosen"