我有以下代码段
public static MvcHtmlString CIDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string modelName)
{
TagBuilder containerDivBuilder = new TagBuilder("div");
if (modelName == null) { throw new ArgumentNullException("modelName"); }
ModelState modelState = helper.ViewData.ModelState[modelName];
ModelErrorCollection modelErrors = (modelState == null) ? null : modelState.Errors;
ModelError modelError = ((modelErrors == null) || (modelErrors.Count == 0)) ? null : modelErrors[0];
var metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
if (metadata.IsRequired)
{
//Code goes here
}
}
,我正在尝试升级到.net core 3.0,但在如何使用ModelExpression方面存在问题。我尝试了几种方法,但我明显遗漏了一些东西。无论如何,这是我的最新尝试。
public static IHtmlContent CIDropDownListFor<TModel, TProperty>(this IHtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IEnumerable<Microsoft.AspNetCore.Mvc.Rendering.SelectListItem> selectList, string modelName)
{
if (modelName == null) { throw new ArgumentNullException("modelName"); }
Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateEntry modelState = helper.ViewData.ModelState[modelName];
Microsoft.AspNetCore.Mvc.ModelBinding.ModelErrorCollection modelErrors = (modelState == null) ? null : modelState.Errors;
Microsoft.AspNetCore.Mvc.ModelBinding.ModelError modelError = ((modelErrors == null) || (modelErrors.Count == 0)) ? null : modelErrors[0];
IModelExpressionProvider meta;
meta.CreateModelExpression(helper.ViewData, expression);
if (meta.IsRequired)
{
//Code goes here
}
一个示例代码将不胜感激。 预先感谢