ASP.Net Core本地化日期格式验证无效
创始人
2024-09-15 18:00:32
0

问题描述:ASP.Net Core中本地化日期格式验证无效。

解决方法:

  1. 确保在Startup.cs文件中正确配置了本地化设置。
public void ConfigureServices(IServiceCollection services)
{
    // 添加本地化支持
    services.AddLocalization(options => options.ResourcesPath = "Resources");
    services.Configure(options =>
    {
        var supportedCultures = new[]
        {
            new CultureInfo("en-US"),
            new CultureInfo("zh-CN")
        };

        options.DefaultRequestCulture = new RequestCulture("zh-CN");
        options.SupportedCultures = supportedCultures;
        options.SupportedUICultures = supportedCultures;
    });

    services.AddMvc()
        .AddViewLocalization()
        .AddDataAnnotationsLocalization();
}
  1. 在Model中使用[DataType(DataType.Date)]属性进行日期类型字段的验证,同时使用[DisplayFormat]属性指定日期的格式。
public class MyModel
{
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime MyDate { get; set; }
}
  1. 在View中使用@Html.EditorFor@Html.TextBoxFor来生成日期输入框,并使用asp-format属性指定日期的格式。
@model MyModel

@Html.LabelFor(m => m.MyDate)
@Html.EditorFor(m => m.MyDate, new { htmlAttributes = new { @class = "form-control", asp_format = "{0:yyyy-MM-dd}" } })
@Html.ValidationMessageFor(m => m.MyDate)
  1. 在View中使用@Html.ValidationSummary来显示日期验证的错误信息。
@Html.ValidationSummary(true, "", new { @class = "text-danger" })

这样就可以正确地进行本地化日期格式的验证了。

相关内容

热门资讯

来一盘!wepoker私人局俱... 来一盘!wepoker私人局俱乐部,wepoker正确养号方法,都是真的有挂(的确有挂)1、上手简单...
实测交流!wepoker怎么设... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
关于!wepoker怎么增加运... 关于!wepoker怎么增加运气,wepoker软件安装包,都是存在有挂(揭秘有挂)1、实时透视辅助...
今日公布!wpk官网下载链接,... 今日公布!wpk官网下载链接,sohoo poker辅助,原来真的有挂(有挂工具)在进入软件靠谱后,...
一分钟揭秘!wepoker透视... 一分钟揭秘!wepoker透视脚本苹果版,hhpoker是正品吗,竟然真的有挂(有挂秘笈)1、完成辅...
查到实测!wpk软件是真的吗,... 查到实测!wpk软件是真的吗,有哪些免费的wpk作弊码,其实存在有挂(证实有挂)1、游戏颠覆性的策略...
玩家攻略推荐!哈糖大菠萝开挂,... 玩家攻略推荐!哈糖大菠萝开挂,we poker游戏下,确实真的有挂(有挂方法)1、免费脚本咨询教程、...
安装程序教程!wepoker模... 安装程序教程!wepoker模拟器哪个好用,hhpoker是内部控制吗,竟然是真的有挂(确实有挂)1...
科普攻略!破解辅助插件wepo... 科普攻略!破解辅助插件wepoker,wepoker透视脚本是什么,好像是真的有挂(真的有挂)1、脚...
分享开挂内幕!wepoker免... 分享开挂内幕!wepoker免费脚本弱密码,xpoker怎么作弊,原来存在有挂(有挂攻略)1、完成w...