|
|
|
@ -1,5 +1,4 @@
|
|
|
|
|
using Infrastructure.Resources;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
|
|
@ -56,7 +55,7 @@ namespace Infrastructure.Extensions
|
|
|
|
|
return new SelectList(values, "Id", "Name", selectedValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static object CreateJson(this ControllerBase controller, DefaultModelMetadata metadata)
|
|
|
|
|
public static object CreateJson(this ControllerBase controller, DefaultModelMetadata metadata, DefaultModelMetadata parent = null)
|
|
|
|
|
{
|
|
|
|
|
if (metadata is null)
|
|
|
|
|
{
|
|
|
|
@ -69,6 +68,7 @@ namespace Infrastructure.Extensions
|
|
|
|
|
dynamic json = new ExpandoObject();
|
|
|
|
|
var dictionary = (IDictionary<string, object>)json;
|
|
|
|
|
//type
|
|
|
|
|
|
|
|
|
|
if (metadata.IsComplexType && metadata.IsCollectionType)
|
|
|
|
|
{
|
|
|
|
|
json.type = "array";
|
|
|
|
@ -112,13 +112,6 @@ namespace Infrastructure.Extensions
|
|
|
|
|
{
|
|
|
|
|
json.title = metadata.DisplayName;
|
|
|
|
|
}
|
|
|
|
|
//if (metadata.IsRequired)
|
|
|
|
|
//{
|
|
|
|
|
// if (metadata.ModelType != typeof(bool))
|
|
|
|
|
// {
|
|
|
|
|
// json.required = true;
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
foreach (var attribute in metadata.Attributes.Attributes)
|
|
|
|
|
{
|
|
|
|
|
if (attribute is DescriptionAttribute descriptionAttribute)
|
|
|
|
@ -127,15 +120,9 @@ namespace Infrastructure.Extensions
|
|
|
|
|
}
|
|
|
|
|
else if (attribute is RequiredAttribute requiredAttribute)
|
|
|
|
|
{
|
|
|
|
|
var localizedString = localizer.GetString(nameof(RequiredAttribute));
|
|
|
|
|
var message = requiredAttribute.ErrorMessage;
|
|
|
|
|
if (!localizedString.ResourceNotFound)
|
|
|
|
|
{
|
|
|
|
|
message = string.Format(localizedString.Value, metadata.DisplayName);
|
|
|
|
|
}
|
|
|
|
|
json.required = new
|
|
|
|
|
{
|
|
|
|
|
message = localizedString.ResourceNotFound ? requiredAttribute.FormatErrorMessage(metadata.DisplayName) : string.Format(localizedString.Value, metadata.DisplayName)
|
|
|
|
|
message = requiredAttribute.GetErrorMessage(localizer, metadata.DisplayName)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else if (attribute is RegularExpressionAttribute regularExpressionAttribute)
|
|
|
|
@ -143,7 +130,7 @@ namespace Infrastructure.Extensions
|
|
|
|
|
json.pattern = new
|
|
|
|
|
{
|
|
|
|
|
regex = regularExpressionAttribute.Pattern,
|
|
|
|
|
message = regularExpressionAttribute.ErrorMessage
|
|
|
|
|
message = regularExpressionAttribute.GetErrorMessage(localizer, metadata.DisplayName)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else if (attribute is DataTypeAttribute dataTypeAttribute)
|
|
|
|
@ -179,37 +166,50 @@ namespace Infrastructure.Extensions
|
|
|
|
|
}
|
|
|
|
|
else if (attribute is MinLengthAttribute minLengthAttribute)
|
|
|
|
|
{//minLength
|
|
|
|
|
json.minLength = minLengthAttribute.Length;
|
|
|
|
|
json.minLength = new
|
|
|
|
|
{
|
|
|
|
|
min = minLengthAttribute.Length,
|
|
|
|
|
message = minLengthAttribute.GetErrorMessage(localizer, metadata.DisplayName)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else if (attribute is MaxLengthAttribute maxLengthAttribute)
|
|
|
|
|
{//maxLength
|
|
|
|
|
json.maxLength = maxLengthAttribute.Length;
|
|
|
|
|
json.maxLength = new
|
|
|
|
|
{
|
|
|
|
|
max = maxLengthAttribute.Length,
|
|
|
|
|
message = maxLengthAttribute.GetErrorMessage(localizer, metadata.DisplayName)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else if (attribute is StringLengthAttribute stringLengthAttribute)
|
|
|
|
|
{//minLength,maxLength
|
|
|
|
|
json.minLength = stringLengthAttribute.MinimumLength;
|
|
|
|
|
json.maxLength = stringLengthAttribute.MaximumLength;
|
|
|
|
|
json.length = new
|
|
|
|
|
{
|
|
|
|
|
min = stringLengthAttribute.MinimumLength,
|
|
|
|
|
max = stringLengthAttribute.MaximumLength,
|
|
|
|
|
message = stringLengthAttribute.GetErrorMessage(localizer, metadata.DisplayName,
|
|
|
|
|
stringLengthAttribute.MinimumLength.ToString(),
|
|
|
|
|
stringLengthAttribute.MaximumLength.ToString())
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else if (attribute is RangeAttribute rangeAttribute)
|
|
|
|
|
{//minimum,maximum
|
|
|
|
|
json.minimum = rangeAttribute.Minimum;
|
|
|
|
|
json.maximum = rangeAttribute.Maximum;
|
|
|
|
|
}
|
|
|
|
|
else if (attribute is ScaffoldColumnAttribute scaffoldColumnAttribute)
|
|
|
|
|
{//scaffold 自定义
|
|
|
|
|
json.scaffold = scaffoldColumnAttribute.Scaffold;
|
|
|
|
|
}
|
|
|
|
|
else if (attribute is HiddenInputAttribute hiddenInputAttribute)
|
|
|
|
|
{//hidden 自定义
|
|
|
|
|
json.hidden = hiddenInputAttribute.DisplayValue;
|
|
|
|
|
}
|
|
|
|
|
else if (attribute is UIHintAttribute uIHintAttribute)
|
|
|
|
|
{//ui 自定义
|
|
|
|
|
json.ui = uIHintAttribute.UIHint.ToLower();
|
|
|
|
|
json.range = new
|
|
|
|
|
{
|
|
|
|
|
minimum = rangeAttribute.Minimum,
|
|
|
|
|
maximum = rangeAttribute.Maximum,
|
|
|
|
|
message = rangeAttribute.GetErrorMessage(localizer, metadata.DisplayName,
|
|
|
|
|
rangeAttribute.Minimum.ToString(),
|
|
|
|
|
rangeAttribute.Minimum.ToString())
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else if (attribute is CompareAttribute compareAttribute)
|
|
|
|
|
{//compare 自定义
|
|
|
|
|
json.compare = compareAttribute.OtherProperty;
|
|
|
|
|
var compareName = parent.Properties.FirstOrDefault(o => o.PropertyName == compareAttribute.OtherProperty)?.DisplayName;
|
|
|
|
|
json.compare = new
|
|
|
|
|
{
|
|
|
|
|
other = compareAttribute.OtherProperty,
|
|
|
|
|
message = compareAttribute.GetErrorMessage(localizer, metadata.DisplayName, compareName)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else if (attribute is RemoteAttribute remoteAttribute)
|
|
|
|
|
{//remote 自定义
|
|
|
|
@ -218,36 +218,67 @@ namespace Infrastructure.Extensions
|
|
|
|
|
{
|
|
|
|
|
url = controller.Url.Action(routeData["action"].ToString(), routeData["controller"].ToString(), new { Area = routeData["area"].ToString() }),
|
|
|
|
|
remoteAttribute.AdditionalFields,
|
|
|
|
|
//remoteAttribute.
|
|
|
|
|
message = remoteAttribute.GetErrorMessage(localizer, metadata.DisplayName)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else if (attribute is ScaffoldColumnAttribute scaffoldColumnAttribute)
|
|
|
|
|
{//scaffold 自定义
|
|
|
|
|
json.scaffold = scaffoldColumnAttribute.Scaffold;
|
|
|
|
|
}
|
|
|
|
|
else if (attribute is HiddenInputAttribute hiddenInputAttribute)
|
|
|
|
|
{//hidden 自定义
|
|
|
|
|
json.hidden = hiddenInputAttribute.DisplayValue;
|
|
|
|
|
}
|
|
|
|
|
else if (attribute is UIHintAttribute uIHintAttribute)
|
|
|
|
|
{//ui 自定义
|
|
|
|
|
json.ui = uIHintAttribute.UIHint.ToLower();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
dynamic properties = new ExpandoObject();
|
|
|
|
|
var propertiesDictionary = (IDictionary<string, object>)properties;
|
|
|
|
|
//var requiredList = new List<string>();
|
|
|
|
|
if (metadata.IsComplexType && !metadata.IsCollectionType && metadata.Properties.Any())
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in metadata.Properties)
|
|
|
|
|
{
|
|
|
|
|
var modelMetadataItem = item as DefaultModelMetadata;
|
|
|
|
|
propertiesDictionary[item.PropertyName] = CreateJson(controller, modelMetadataItem);
|
|
|
|
|
//if (modelMetadataItem.IsRequired)
|
|
|
|
|
//{
|
|
|
|
|
// requiredList.Add(item.PropertyName);
|
|
|
|
|
//}
|
|
|
|
|
propertiesDictionary[item.PropertyName] = CreateJson(controller, modelMetadataItem, metadata);
|
|
|
|
|
}
|
|
|
|
|
json.properties = properties;
|
|
|
|
|
// if (requiredList.Any())
|
|
|
|
|
// {
|
|
|
|
|
// json.required = requiredList;
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
return json;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ex.PrintStack();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string GetErrorMessage(this ValidationAttribute attribute, IStringLocalizer localizer, params string[] args)
|
|
|
|
|
{
|
|
|
|
|
var localizedString = localizer.GetString(attribute.GetType().Name);
|
|
|
|
|
//return localizedString.ResourceNotFound ? attribute.FormatErrorMessage(args[0]) : string.Format(localizedString.Value, args);
|
|
|
|
|
var format = attribute.ErrorMessage;
|
|
|
|
|
if (localizedString.ResourceNotFound)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(format))
|
|
|
|
|
{
|
|
|
|
|
format = attribute.GetType().GetProperty("ErrorMessageString", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(attribute) as string;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
format = localizedString.Value;
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return string.Format(format, args);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ex.PrintStack();
|
|
|
|
|
}
|
|
|
|
|
return format;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|