<i id='L9tBr'><tr id='L9tBr'><dt id='L9tBr'><q id='L9tBr'><span id='L9tBr'><b id='L9tBr'><form id='L9tBr'><ins id='L9tBr'></ins><ul id='L9tBr'></ul><sub id='L9tBr'></sub></form><legend id='L9tBr'></legend><bdo id='L9tBr'><pre id='L9tBr'><center id='L9tBr'></center></pre></bdo></b><th id='L9tBr'></th></span></q></dt></tr></i><div id='L9tBr'><tfoot id='L9tBr'></tfoot><dl id='L9tBr'><fieldset id='L9tBr'></fieldset></dl></div>
<legend id='L9tBr'><style id='L9tBr'><dir id='L9tBr'><q id='L9tBr'></q></dir></style></legend>

    • <bdo id='L9tBr'></bdo><ul id='L9tBr'></ul>
    <tfoot id='L9tBr'></tfoot>
  • <small id='L9tBr'></small><noframes id='L9tBr'>

        ASP.NET 自定义模型绑定:DateTime

        ASP.NET Custom Model Binding: DateTime(ASP.NET 自定义模型绑定:DateTime)
          <bdo id='AFE17'></bdo><ul id='AFE17'></ul>
            1. <i id='AFE17'><tr id='AFE17'><dt id='AFE17'><q id='AFE17'><span id='AFE17'><b id='AFE17'><form id='AFE17'><ins id='AFE17'></ins><ul id='AFE17'></ul><sub id='AFE17'></sub></form><legend id='AFE17'></legend><bdo id='AFE17'><pre id='AFE17'><center id='AFE17'></center></pre></bdo></b><th id='AFE17'></th></span></q></dt></tr></i><div id='AFE17'><tfoot id='AFE17'></tfoot><dl id='AFE17'><fieldset id='AFE17'></fieldset></dl></div>

              <legend id='AFE17'><style id='AFE17'><dir id='AFE17'><q id='AFE17'></q></dir></style></legend>
                  <tbody id='AFE17'></tbody>
                <tfoot id='AFE17'></tfoot>

                1. <small id='AFE17'></small><noframes id='AFE17'>

                2. 本文介绍了ASP.NET 自定义模型绑定:DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  问题

                  此时我遇到了一个问题,即我的 Get 操作试图以不同格式读取发送的 DateTime 参数.

                  At this point I have a problem where my Get action is trying to read a DateTime parameter in a diferent format that is sent.

                  虽然发送的 DateTime 格式如下:0:dd/MM/yyyy获取操作期望:0:MM/dd/yyyy

                  While the DateTime sent has this format: 0:dd/MM/yyyy The Get Actions expects: 0:MM/dd/yyyy

                  解决方案(也许)

                  为了更改 Get 操作所期望的内容,我使用了自定义模型绑定.

                  In order to change what the Get action is expecting I'm using a Custom Model Binding.

                  GET 操作

                      public async Task<IActionResult> Details(int? id, [ModelBinder(typeof(PModelBinder))]DateTime date)
                  

                  ModelBinder 类

                  现在这里缺少一些东西,我不知道如何正确完成它:

                  Now here are a few things that are missing and I don't know how to complete it properly:

                  public class PModelBinder : IModelBinder
                  {
                      public Task BindModelAsync(ModelBindingContext bindingContext)
                      {
                          string theDate = bindingContext.HttpContext.Request.QueryString["date"]; 
                          //What should I write inside the []? 
                          //I've tried QueryString["date"] which is the name of the parameter but it says is wrong
                          DateTime dt = new DateTime();
                          bool success = DateTime.TryParse(date); //Should I apply ParseExact? How should I do it?
                          if (success)
                          {
                              return new //what should I be returning here? dt?
                          }
                  
                      }
                  }
                  

                  由于我刚刚开始了解自定义模型绑定,因此我在上面的代码中将几个问题标记为注释.希望有人能给我一些建议.

                  I've several questions marked as comments in the code above since I'm just starting to understand Custom Model Binding. Hope anyone can give me some advice.

                  我正在关注这篇文章:

                  https://weblogs.asp.net/melvynharbour/mvc-modelbinder-和本地化

                  但它来自 2008 年!!!,虽然这似乎是有效的,因为这正是我的 GET 操作(不同的日期格式)遇到的问题

                  But it's from 2008!!!, Although it seems valid since it's exactly the problem I'm having with my GET Action (diferent date formats)

                  更新:附加信息

                  参数日期定义为:

                      [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
                      public DateTime FechaLInicioLiq { get; set; }
                  

                  调用 GET 操作时构建的 URL 具有日期参数的这种结构:

                  and the URL build when calling that GET Action has this structure for the date parameter:

                  date=10%2F11%2F2017%200%3A00%3A00
                  

                  推荐答案

                  你的模型绑定器实现有几个问题:

                  You have several issues with your model binder implementation:

                  1. 不要硬编码参数名称(date).请改用 bindingContext.ModelName.
                  2. 如果没有实际提供价值,您应该处理这种情况.您可以通过将 IValueProvider.GetValue() 的结果与 ValueProviderResult.None 进行比较来检查它.
                  1. Do not hardcode parameter name (date). Use bindingContext.ModelName instead.
                  2. You should handle situation if value was not actually provided. You could check it by comparing result of IValueProvider.GetValue() with ValueProviderResult.None.

                  这是完成您需要的示例 DateTime 模型绑定器:

                  Here is sample DateTime model binder that accomplish what you need:

                  public class DateTimeModelBinder : IModelBinder
                  {
                      private readonly IModelBinder baseBinder = new SimpleTypeModelBinder(typeof(DateTime));
                  
                      public Task BindModelAsync(ModelBindingContext bindingContext)
                      {
                          var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
                          if (valueProviderResult != ValueProviderResult.None)
                          {
                              bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);
                  
                              var valueAsString = valueProviderResult.FirstValue;
                  
                              //  valueAsString will have a string value of your date, e.g. '31/12/2017'
                              var dateTime = DateTime.ParseExact(valueAsString, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                              bindingContext.Result = ModelBindingResult.Success(dateTime);
                  
                              return Task.CompletedTask;
                          }
                  
                          return baseBinder.BindModelAsync(bindingContext);
                      }
                  }
                  

                  这篇关于ASP.NET 自定义模型绑定:DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  C# Attributes and their uses(C# 属性及其用途)
                  C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)
                3. <i id='QNN1k'><tr id='QNN1k'><dt id='QNN1k'><q id='QNN1k'><span id='QNN1k'><b id='QNN1k'><form id='QNN1k'><ins id='QNN1k'></ins><ul id='QNN1k'></ul><sub id='QNN1k'></sub></form><legend id='QNN1k'></legend><bdo id='QNN1k'><pre id='QNN1k'><center id='QNN1k'></center></pre></bdo></b><th id='QNN1k'></th></span></q></dt></tr></i><div id='QNN1k'><tfoot id='QNN1k'></tfoot><dl id='QNN1k'><fieldset id='QNN1k'></fieldset></dl></div>

                  <small id='QNN1k'></small><noframes id='QNN1k'>

                        <tbody id='QNN1k'></tbody>

                        <legend id='QNN1k'><style id='QNN1k'><dir id='QNN1k'><q id='QNN1k'></q></dir></style></legend>

                          <tfoot id='QNN1k'></tfoot>

                          • <bdo id='QNN1k'></bdo><ul id='QNN1k'></ul>