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

编程入门 行业动态 更新时间:2024-10-27 21:22:56
本文介绍了ASP.NET自定义模型绑定:DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

问题

The problem

这时我遇到一个问题,我的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 Get Actions预期:0:MM/dd/yyyy

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

解决方案(也许)

The solution (maybe)

为了更改获取"操作的期望,我使用的是自定义模型绑定".

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.

我正在关注这篇文章:

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

但这是从 2008 !!! 开始的,尽管它似乎是有效的,因为这正是我的GET Action(不同的日期格式)所遇到的问题

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)

更新:附加信息

Update: aditional information

参数日期定义为:

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

以及调用该GET Action的URL构建具有针对date参数的以下结构:

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:

  • 请勿对参数名称(date)进行硬编码.请使用bindingContext.ModelName.
  • 如果没有实际提供价值,您应该处理情况.您可以通过比较IValueProvider.GetValue()和ValueProviderResult.None的结果来检查它.
  • Do not hardcode parameter name (date). Use bindingContext.ModelName instead.
  • 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

    本文发布于:2023-11-16 07:08:23,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1602092.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:自定义   绑定   模型   ASP   NET

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!