Как можно Я получаю атрибут DataTypeAttribute свойства, опуская аргументContainerType?
Это мой текущий код:
Код: Выделить всё
public static IHtmlContent MyEditorFor(this IHtmlHelper html,
Expression expression,
object moreData,
Type? containerType = null)
{
var ep = html.ViewContext.HttpContext.RequestServices.GetService(typeof(ModelExpressionProvider)) as ModelExpressionProvider;
var bindName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ep.GetExpressionText(expression));
if (containerType == null) containerType = html.ViewData.Model.GetType();
var dataType = GetDataType(containerType, bindName);
//etc...
}
private static DataType GetDataType(Type containerType, string bindName)
{
var result = DataType.Text;
var propName = bindName.Split('.')[^1];
var prop = containerType.GetProperty(propName);
var att = prop.GetCustomAttributes(typeof(DataTypeAttribute), true);
var attProperty = typeof(DataTypeAttribute).GetProperty(nameof(DataTypeAttribute.DataType));
result = (DataType)(attProperty.GetValue(att[0]) ?? DataType.Text);
return result;
}
Код: Выделить всё
@Html.EditorForEx(x => x.LinkedTable[i].MyDate, new { placeholder = "Test Date" }, Model.LinkedTable[i].GetType())
- Проверки на null опущены для простоты
Подробнее здесь: https://stackoverflow.com/questions/792 ... yeditorfor
Мобильная версия