views:

61

answers:

1

I am working on an ASP.NET MVC application which has a model for a business, linked to another model (business trading hours) in a one-to-many relationship.

When creating this entity we have a form where we would like to provide a fixed set of the BusinessTradingHours entities, so I have used something like the following (kind of what I am used to with Rails):

<!-- for Monday -->
<select id="TradingHours[1][OpenHours]" name="TradingHours[1][OpenHours]"><option>06:00</option></select>
<select id="TradingHours[1][CloseHours]" name="TradingHours[1][CloseHours]"><option>06:00</option></select>
<input id="TradingHours[1][IsClosed]" name="TradingHours[1][IsClosed]" type="checkbox" value="true" />

<!-- for Tuesday -->
<select id="TradingHours[2][OpenHours]" name="TradingHours[2][OpenHours]"><option>06:00</option></select>
<select id="TradingHours[2][CloseHours]" name="TradingHours[2][CloseHours]"><option>06:00</option></select>
<input id="TradingHours[2][IsClosed]" name="TradingHours[2][IsClosed]" type="checkbox" value="true" />

From my experience in Rails (which I know is not the same...) I would kind of expect to be able to do something like var tradingHours = Request.Form["TradingHours"]; which would give me an Array, which I could then iterate over... is this possible?

Otherwise, how else would this be accomplished?

+4  A: 

See this post from Scott Hanselman:

ASP.NET Wire Format for Model Binding to Arrays, Lists, Collections, Dictionaries

Robert Harvey
Actually, anything by Scott Hanselman should be read.
griegs
OMG why didn't I see/remember this... I read Scott's blog like.. religiously... thanks for pointing it out :)
Matthew Savage