Created
July 13, 2019 09:45
-
-
Save DmitrySikorsky/4e76438d8abb8f01ff1962880b838b6f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public override void OnActionExecuting(ActionExecutingContext actionExecutingContext) | |
{ | |
base.OnActionExecuting(actionExecutingContext); | |
this.HandleViewModelMultilingualProperties(actionExecutingContext); | |
} | |
private void HandleViewModelMultilingualProperties(ActionExecutingContext actionExecutingContext) | |
{ | |
ViewModelBase viewModel = this.GetViewModelFromActionExecutingContext(actionExecutingContext); | |
if (viewModel == null) | |
return; | |
ICultureRepository cultureRepository = this.HttpContext.RequestServices.GetService<ICultureRepository>(); | |
try | |
{ | |
foreach (PropertyInfo propertyInfo in this.GetLocalizablePropertiesFromViewModel(viewModel)) | |
{ | |
this.ModelState.Remove(propertyInfo.Name); | |
bool hasRequiredAttribute = propertyInfo.CustomAttributes.Any(ca => ca.AttributeType == typeof(RequiredAttribute)); | |
foreach (Culture culture in cultureRepository.GetAll()) | |
{ | |
string identity = propertyInfo.Name + culture.Code; | |
string value = this.Request.Form[identity]; | |
this.ModelState.SetModelValue(identity, value, value); | |
if (hasRequiredAttribute && string.IsNullOrEmpty(value)) | |
{ | |
this.ModelState[identity].ValidationState = ModelValidationState.Invalid; | |
this.ModelState[identity].Errors.Add(string.Empty); | |
} | |
else this.ModelState[identity].ValidationState = ModelValidationState.Valid; | |
} | |
} | |
} | |
catch { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment