简体   繁体   中英

How can I bind a list model which contains a complex type in MVC4?

I have a form with input elements like this:

...
<input type="hidden" name="orders[0].ID" value="1">
<input type="text" name="orders[0].Name" value="Some name">
<input type="date" name="orders[0].Date" value="09/08/2014">
<input type="time" name="orders[0].Time" value="5:16 AM">
...
<input type="hidden" name="orders[1].ID" value="2">
<input type="text" name="orders[1].Name" value="Some name2">
<input type="date" name="orders[1].Date" value="09/08/2014">
<input type="time" name="orders[1].Time" value="5:16 AM">
...
<input type="hidden" name="orders[2].ID" value="2">
<input type="text" name="orders[2].Name" value="Some name2">
<input type="date" name="orders[2].Date" value="09/08/2014">
<input type="time" name="orders[2].Time" value="5:16 AM">
...

So, when the form submits it should bind to an action like this:

public ActionResult Update(IEnumerable<Order> orders){}

The thing is, my view has the date and the time as separate input elements, but the Order class has one single DateTime property, which should combine the two. Is there any way to bind this in a way that is simpler than creating a new Modelbinder?

You can use javascript to handle onsubmit event and merge data from those two fields into one that will be parsed as DateTime. Do you need some code to see how it could be done?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM