DisplayFormat ApplyFormatInEditMode [英] DisplayFormat ApplyFormatInEditMode

查看:344
本文介绍了DisplayFormat ApplyFormatInEditMode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC 3在C#中,我有一个类具有这种属性。

I use MVC 3 in C#, I have a class with this Attribute

[DisplayFormat(DataFormatString = "{0:dd MMM yyyy}", ApplyFormatInEditMode = true)]

我想执行验证,当用户在编辑模式

在数据库中的数据存储与键入日期时间此格式

Data in database is stored with type datetime formatted like this

  6/15/2009 1:45:30 PM

我收到此错误
    错误字符串是格式不正确

I receive this error Error String is not in correct format

我beleive问题是在

I beleive the problem is in

DataFormatString ={0:DD MMM YYYY}

DataFormatString = "{0:dd MMM yyyy}"

任何想法如何解决?

推荐答案

DisplayFormat 属性是字面上仅用于显示值。如果设置了 ApplyFormatInEditMode 所有会做的格式文本框(用于编辑)显示时,也适用于您的数据的内容。它有没有关系的验证。

The DisplayFormat attribute is literally only for displaying the value. If you set the ApplyFormatInEditMode all it will do is also apply the format to the contents of your data when displayed in a textbox (intended for Editing). It's got nothing to do with validation.

如果你想用你指定你可能必须创建格式验证输入自己的 ValidationAttribute ,并使用的DateTime .ParseExact()尝试以确保它符合你希望的格式。唯一的缺点是,除非你把它写它不会有一个附带的客户端验证逻辑。

If you want to validate the input using the format you've specified you'll likely have to create your own ValidationAttribute, and use DateTime.ParseExact() to attempt to ensure it meets the format you're expecting. The only draw back is it will not have an accompanying client side validation logic unless you write it.

我没有测试此thuroughly,但它应该给你一个开始。

I have not tested this thuroughly, but it should give you a start.

public class DateTimeFormatAttribute : ValidationAttribute
{
   public int Format { get; set; }

   public override bool IsValid(object value)
   {
        if (value == null)
            return true;

        DateTime val;
        try
        {
            val = DateTime.ParseExact(value.ToString(), Format, null);
        }
        catch(FormatException)
        {
            return false;
        }

        //Not entirely sure it'd ever reach this, but I need a return statement in all codepaths
        return val != DateTime.MinValue;
   }
}

然后,它只是一个使用它的问题。
[DateTimeFormat(格式=DD MMM YYYY)]

更新:对不起,我donn't想我看清楚你的问题。它在抱怨上回发数据的原因是因为你试图使用的格式不是一个标准。你可能会关闭实施共同的日期选取器的一个在线填充字段时使用,而不是让它手工​​编辑或期待这样的非标准格式的更好。自定义显示格式是伟大的显示,但如果你要使用编辑模式自定义格式,默认 DateTime.Parse 不明白你必须写你自己ModelBinder的,我相信,这件事情我没有做,或者你可以在你的视图模型中的数据类型更改为字符串,而自己在操作方法解析(你仍然可以使用我在这种情况下所提供的验证)。为了摆脱你的错误的(但也将删除自定义格式编辑模式时),你就必须删除 ApplyFormatInEditMode 属性。

UPDATE: Sorry I donn't think I clearly read your question. The reason it's complaining about the data on postback is because the format you're trying to use is not a standard one. You might be better off implementing one of the common date pickers online to use when populating the field rather than letting it be hand edited or expecting a non-standard format like this. Custom display formats are great for displaying, but if you want to use a custom format for edit mode that the default DateTime.Parse doesn't understand you'd have to write your own ModelBinder I believe, and that's something I'd not done, alternatively you could change the data type on your viewmodel to string and the parse it yourself in the action method (you could still use the validator I provided in this case). To get rid of your error (but will also remove your custom format when in edit mode) you'd have to remove the ApplyFormatInEditMode property.

这篇关于DisplayFormat ApplyFormatInEditMode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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