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

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

问题描述

在我的 ASP.NET MVC 站点中,部分功能允许用户输入特定场所开放的时间.

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

我决定将这些小时数存储在我的数据库中的 VenueHours 表中,并与 Venues 表具有 FK-to-PK 关系,以及 DayOfWeek、OpeningTime 和 ClosingTime 参数.

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.

在我的视图中,我想让用户只输入他们知道的时间;换句话说,有些天数可能无法填写场地.我正在考虑创建用户可以选中的复选框,以便为复选框所属的 DayOfWeek 启用 OpeningTime 和 ClosingTime 字段.

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.

我的问题与如何将此信息传递给我的 HttpPost 控制器操作有关.

因为我知道可以在 (7) 中传递的最大天数,我当然可以将 7 个可为空的 VenueHour 参数写入我的 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 发布了一个 非常好关于默认模型绑定器如何处理集合、数组和字典的文章.如果您不想编写自定义模型绑定器,那就太好了,尽管编写自定义模型绑定器没什么大不了的.

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天全站免登陆