简体   繁体   English

如何在 MVC4 中绑定包含复杂类型的列表模型?

[英]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.问题是,我的视图将日期时间作为单独的输入元素,但 Order 类只有一个DateTime属性,应该将两者结合起来。 Is there any way to bind this in a way that is simpler than creating a new Modelbinder?有没有办法以比创建新的 Modelbinder 更简单的方式来绑定它?

You can use javascript to handle onsubmit event and merge data from those two fields into one that will be parsed as DateTime.您可以使用 javascript 来处理 onsubmit 事件并将这两个字段中的数据合并为一个将被解析为 DateTime 的字段。 Do you need some code to see how it could be done?你需要一些代码来看看它是如何完成的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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