ASP.NET MVC模型绑定到列表 [英] ASP.NET MVC Model Binding into a List

查看:423
本文介绍了ASP.NET MVC模型绑定到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET MVC网站中,功能的一部分允许用户输入某个场地打开的时间。



我决定存储这些时间在我的数据库中的VenueHours表中,与Venues表中有一个FK到PK的关系,以及DayOfWeek,OpeningTime和ClosingTime参数。



我的观点,我想允许用户只输入他们知道的时间;换句话说,有些日子可能不会填写在场地。我正在考虑创建用户可以检查的复选框,以启用该复选框所属的DayOfWeek的OpeningTime和ClosingTime字段。






我的问题涉及如何将此信息传递给我的HttpPost控制器操作



由于我知道最大天数可以通过(7),我当然可以在我的Action中写入7个可空的VenueHour参数,但我确定有一个更好的方法。



我可以不知何故将查看信息绑定到传递给我的Action 的列表中?如果我遇到一个不能限制用户填写多少信息的情况,这也将帮助我。

解决方案

Scott Hanselman发布了关于默认模型binder如何处理集合,数组的非常好的文章和字典。如果您不想编写自定义模型绑定器,那么很好,尽管编写自定义模型绑定不是一件大事。


In my ASP.NET MVC site, part of a feature allows the user to enter the hours when a certain venue is open.

I've decided to store these hours in a VenueHours table in my database, with a FK-to-PK relationship to a Venues table, as well as DayOfWeek, OpeningTime, and ClosingTime parameters.

In my View, I want to allow the user to only input the times they know about; in other words, some days may not be filled in for a Venue. I'm thinking of creating checkboxes that the user can check to enable the OpeningTime and ClosingTime fields for the DayOfWeek that the checkbox belongs to.


My question relates to how to pass this information to my HttpPost Controller Action.

As I know the maximum amount of Days that can be passed in (7), I could of course just write 7 nullable VenueHour parameters into my Action, but I'm sure there's a better way.

Can I somehow bind the View information into a List that is passed to my Action? This will also help me if I run into a scenario where there is no limit to how much information the user can fill in.

解决方案

Scott Hanselman posted a very good article about how the default model binder deals with collections, arrays and dictionaries. It's great if you don't want to write a custom model binder, although writing a custom model binder isn't that big a deal.

这篇关于ASP.NET MVC模型绑定到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆