Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Dependencies(Nuget): DNTPersianUtils.Core
- public class PersianDateModelBinder : IModelBinder
- {
- public Task BindModelAsync(ModelBindingContext bindingContext)
- {
- var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
- DateTime? dt;
- try
- {
- var value = valueResult.FirstValue;
- var isValidPersianDateTime = value != null && DNTPersianUtils.Core.PersianDateTimeUtils.IsValidPersianDateTime(value);
- if (isValidPersianDateTime)
- {
- dt = DNTPersianUtils.Core.PersianDateTimeUtils.ToGregorianDateTime(value);
- }
- else
- {
- dt = null;
- }
- }
- catch (Exception e)
- {
- dt = null;
- }
- if (dt == null)
- {
- bindingContext.ModelState.TryAddModelError(bindingContext.ModelName, "Jalali date is not valid");
- }
- else if (Nullable.GetUnderlyingType(bindingContext.ModelType) == typeof(DateTime))
- {
- bindingContext.Result = ModelBindingResult.Success(dt);
- }
- else
- {
- bindingContext.Result = ModelBindingResult.Success(dt.Value);
- }
- return Task.CompletedTask;
- }
- public class PersianDateModelBinderProvider : IModelBinderProvider
- {
- private readonly IModelBinder _binder = new PersianDateModelBinder();
- public IModelBinder GetBinder(ModelBinderProviderContext context)
- {
- var isDateTimeNullableType = context.Metadata.ModelType == typeof(DateTime?);
- var isDateTimeType = context.Metadata.ModelType == typeof(DateTime);
- return isDateTimeType || isDateTimeNullableType ? _binder : null;
- }
- }
- }
- How To Use:
- In Startup.cs:
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddMvcCore(options =>
- {
- options.ModelBinderProviders.Insert(0, new PersianDateModelBinderProvider());
- });
- }
- In Actions:
- public async Task<IActionResult> SampleAction(DateTime birthDate)
- {
- if (ModelState.IsValid)
- {
- //birthDate is valid
- }
- else
- {
- birthDate is wrong
- }
- ...
- }
- //or with nullable datetime:
- public async Task<IActionResult> SampleAction(DateTime? birthDate)
- {
- if (ModelState.IsValid)
- {
- //birthDate is valid
- }
- else
- {
- //birthDate is wrong
- }
- ...
- }
Add Comment
Please, Sign In to add comment