Skip to content

Instantly share code, notes, and snippets.

@DmitrySikorsky
Created July 13, 2019 09:45
Show Gist options
  • Save DmitrySikorsky/4e76438d8abb8f01ff1962880b838b6f to your computer and use it in GitHub Desktop.
Save DmitrySikorsky/4e76438d8abb8f01ff1962880b838b6f to your computer and use it in GitHub Desktop.
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