【问题标题】:.NET 5 (Core) MVC - Model Binding DateTime using en-GB not working on GET but works fine on POST.NET 5 (Core) MVC - 使用 en-GB 的模型绑定 DateTime 不能在 GET 上工作,但在 POST 上工作正常
【发布时间】:2021-03-26 02:58:52
【问题描述】:

我有一个已配置的新 .NET 5 Web 应用程序:

 public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<RequestLocalizationOptions>(
            opts =>
            {
                var supportedCultures = new List<CultureInfo> {new CultureInfo("en-GB"),};

                opts.SupportedCultures = supportedCultures;
                opts.SupportedUICultures = supportedCultures;
                opts.DefaultRequestCulture = new RequestCulture(culture: "en-GB", uiCulture: "en-GB");

            });
        services.AddControllersWithViews();
    }

然后我正在使用:

        app.UseRequestLocalization();
        app.UseRouting();

我的测试表格:

<form method="get" asp-action="TestGet" asp-controller="Home">
    <h2>test get</h2>
     <input type="text" id="From" name="From" value="15/12/2020">
    <button type="submit">go</button>
</form>

<form method="post" asp-action="TestPost" asp-controller="Home">
    <h2>test post</h2>
    <input type="text" id="From" name="From" value="15/12/2020">
    <button type="submit">go</button>
</form>

我的行动:

[HttpGet]
        public RedirectResult TestGet(DateTime from)
        {
            var me = from;
            return Redirect("/");
        }
        
        [HttpPost]
        public RedirectResult TestPost(DateTime from)
        {
            var me = from;
            return Redirect("/");
        }

预期的结果是日期在两种情况下都是绑定的,但这仅适用于邮寄。获取日期未设置。

我做错了什么?

【问题讨论】:

    标签: .net asp.net-mvc .net-core


    【解决方案1】:

    好的,找到问题了。它的设计是不变的 - 因此允许共享查询字符串。

    在此详述问题:https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-5.0#globalization-behavior-of-model-binding-route-data-and-query-strings

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 2016-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多