2 love84385654 love84385654 于 2017.01.03 09:29 提问

HtmlHelper扩展ModelState取不出值

代码如下,

 protected internal string GetFieldNameTValue(ExpressionFuncTModel, TValue expression)
        {
            ModelMetadata modelMetadataByExpression = ModelMetadata.FromLambdaExpressionTModel, TValue(expression, ((HtmlHelperTModel)HtmlHelperExtension.HtmlHelper).ViewData);
            string expressionText = ExpressionHelper.GetExpressionText(expression);
            return HtmlHelperExtension.HtmlHelper.ViewData.TemplateInfo.GetFullHtmlFieldName(expressionText);
        }
                 protected internal dynamic GetModelStateValueTValue(ExpressionFuncTModel, TValue expression)
        {
            string key = this.GetFieldName(expression);
            ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpressionTModel, TValue(expression, ((HtmlHelperTModel)HtmlHelperExtension.HtmlHelper).ViewData);
            ModelState modelState;
            if (HtmlHelperExtension.HtmlHelper.ViewData.ModelState.TryGetValue(key, out modelState) && modelState.Value != null)
            {
                return modelState.Value.ConvertTo(modelMetadata.Model.GetType(), null);
            }
            else
                return null;
        }

取出的modelState永远是null ,是不是漏了什么?

1个回答

caozhy
caozhy   Ds   Rxr 2017.01.03 11:00
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!