将日期时间分解为 ASP.NET MVC 形式的组成部分

发布于 2024-09-02 16:22:49 字数 759 浏览 12 评论 0原文

我在网上不断地搜索这个问题,但没有找到任何东西——这很令人惊讶,因为我认为这是一个很常见的情况!

基本上,在我的模型上,我有一个 DateTime 字段,我希望用户通过表单填充该字段。我正在使用 Html 助手来呈现表单的所有其他部分(以及验证)

所以这个问题分为两部分...

Html 助手

首先,有没有办法使用 Html 助手来分割 DateTime< /code> 字段将呈现为日期的三个组成部分:日、月、年(因为我不关心时间部分)。这可以呈现为文本框、下拉列表或两者的组合。

模型绑定

然后,当发布表单时,绑定回模型的最佳方法是什么?我已经看到了 Scott Hanselmann 的解决方案,但对于我来说,它似乎有点臃肿需要 - 我希望有一个稍微更优雅的解决方案。是否建议扩展 DefaultModelBinder 并将其设置为默认绑定器(因为所有日期都将以这种方式处理)或编写一个实现 IModelBionder 的类并将其设置为默认绑定器DateTime 类型的活页夹?

感谢您提前提供的所有帮助:-) 我很喜欢 MVC,但是如此微不足道的事情却引起如此多的头痛,这让我很生气!

i have searched the web relentlessly for this and have not found anything - which is surprising because i would think it is such a common scenario!

Basically, on my model i have a DateTime field which i wish the user to populate through a form. I am using the Html helper to render all other parts of the form (along with validation)

So this question is in two parts...

Html Helper

Firstly, is there any way to use the Html helper to split the DateTime field to be rendered as the three constituent parts of a date: day, month, year (since i do not care about the time part). This could be rendered as text boxes, drop down lists or a combination of both.

Model Binding

And then when the form is posted, what is the best approach for binding back up to the model? I have seen Scott Hanselmann's solution to this, but it seems a little bloated for what i need - i was hoping for a slightly more elegant solution. Is it recommended to extend DefaultModelBinder and set that as default binder (since all dates would be handled in this way) or write a class that implements IModelBionder and set it as the default binder for the DateTime type?

Thanks for all the help in advance :-) i'm loving MVC but it's infuriating me that something so trivial is causing so much headaches!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只想待在家 2024-09-09 16:22:49

我认为我已经找到了一个不错的解决方案,所以我将为将来遇到这个问题的任何人提供答案!

关于 Html 助手,我完全忽略了创建扩展方法!所以最终当我想到时,我编写了一个扩展方法,它基本上调用 Html 帮助器的其他方法来提供三个字段(是否使用文本输入的下拉菜单是您的选择)

    public static string DateTime(this HtmlHelper helper, string name)
    {
        StringBuilder builder = new StringBuilder();
        string dayName = name + ".Day";
        string monthName = name + ".Month";
        string yearName = name + ".Year";

        builder.Append(helper.TextBox(dayName));
        builder.Append(helper.DropDownList(monthName, typeof (Months)));
        builder.Append(helper.TextBox(yearName));

        return builder.ToString();
    }

至于表单发布后的绑定,我只是创建了一个实现 IModelBinder 的类,并将其设置为应用程序启动时的默认 DateTime 类型。如果需要不同的绑定方法,则可以在控制器操作级别覆盖它。这本质上是我在问题中链接到的斯科特活页夹的简单版本。

目前,它就像一个魅力(如果有点简单),但我相信对于其他对这个问题感到困惑的人来说,这将是足够的基础!

think i've worked out a decent solution to this, so i will provide an answer for any who stumble across this in future!

With regards to the Html helper, i somehow completely overlooked creating an extension method! So eventually when it occurred to me, i wrote an extension method which basically makes calls to other methods of the Html helper to provide three fields (whether you use drop down of text inputs is your choice)

    public static string DateTime(this HtmlHelper helper, string name)
    {
        StringBuilder builder = new StringBuilder();
        string dayName = name + ".Day";
        string monthName = name + ".Month";
        string yearName = name + ".Year";

        builder.Append(helper.TextBox(dayName));
        builder.Append(helper.DropDownList(monthName, typeof (Months)));
        builder.Append(helper.TextBox(yearName));

        return builder.ToString();
    }

As for the binding after a form post, i simply created a class which implemented IModelBinder and set it as default for DateTime type on application start. This can then be overridden at a controller action level should a different method of binding be required. This was essentially a simpler version of Scott's binder which i linked to in my question.

Works like a charm at the moment (if a little simplistic), but i'm sure it'll be enough of a foundation for anyone else who is mystified by this problem!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文