Example #1
0
        /// <summary>
        /// Gets <see cref="ViewDataInfo"/> for named <paramref name="expression"/> in given
        /// <paramref name="viewData"/>.
        /// </summary>
        /// <param name="viewData">
        /// The <see cref="ViewDataDictionary"/> that may contain the <paramref name="expression"/> value.
        /// </param>
        /// <param name="expression">Expression name, relative to <c>viewData.Model</c>.</param>
        /// <returns>
        /// <see cref="ViewDataInfo"/> for named <paramref name="expression"/> in given <paramref name="viewData"/>.
        /// </returns>
        public static ViewDataInfo Eval(ViewDataDictionary viewData, string expression)
        {
            if (viewData == null)
            {
                throw new ArgumentNullException(nameof(viewData));
            }

            // While it is not valid to generate a field for the top-level model itself because the result is an
            // unnamed input element, do not throw here if full name is null or empty. Support is needed for cases
            // such as Html.Label() and Html.Value(), where the user's code is not creating a name attribute. Checks
            // are in place at higher levels for the invalid cases.
            var fullName = viewData.TemplateInfo.GetFullHtmlFieldName(expression);

            // Given an expression "one.two.three.four" we look up the following (pseudo-code):
            //  this["one.two.three.four"]
            //  this["one.two.three"]["four"]
            //  this["one.two"]["three.four]
            //  this["one.two"]["three"]["four"]
            //  this["one"]["two.three.four"]
            //  this["one"]["two.three"]["four"]
            //  this["one"]["two"]["three.four"]
            //  this["one"]["two"]["three"]["four"]

            // Try to find a matching ViewData entry using the full expression name. If that fails, fall back to
            // ViewData.Model using the expression's relative name.
            var result = EvalComplexExpression(viewData, fullName);

            if (result == null)
            {
                if (string.IsNullOrEmpty(expression))
                {
                    // Null or empty expression name means current model even if that model is null.
                    result = new ViewDataInfo(container: viewData, value: viewData.Model);
                }
                else
                {
                    result = EvalComplexExpression(viewData.Model, expression);
                }
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// Gets <see cref="ViewDataInfo"/> for named <paramref name="expression"/> in given
        /// <paramref name="viewData"/>.
        /// </summary>
        /// <param name="viewData">
        /// The <see cref="ViewDataDictionary"/> that may contain the <paramref name="expression"/> value.
        /// </param>
        /// <param name="expression">Expression name, relative to <c>viewData.Model</c>.</param>
        /// <returns>
        /// <see cref="ViewDataInfo"/> for named <paramref name="expression"/> in given <paramref name="viewData"/>.
        /// </returns>
        public static ViewDataInfo Eval(ViewDataDictionary viewData, string expression)
        {
            if (viewData == null)
            {
                throw new ArgumentNullException(nameof(viewData));
            }

            // While it is not valid to generate a field for the top-level model itself because the result is an
            // unnamed input element, do not throw here if full name is null or empty. Support is needed for cases
            // such as Html.Label() and Html.Value(), where the user's code is not creating a name attribute. Checks
            // are in place at higher levels for the invalid cases.
            var fullName = viewData.TemplateInfo.GetFullHtmlFieldName(expression);

            // Given an expression "one.two.three.four" we look up the following (pseudo-code):
            //  this["one.two.three.four"]
            //  this["one.two.three"]["four"]
            //  this["one.two"]["three.four]
            //  this["one.two"]["three"]["four"]
            //  this["one"]["two.three.four"]
            //  this["one"]["two.three"]["four"]
            //  this["one"]["two"]["three.four"]
            //  this["one"]["two"]["three"]["four"]

            // Try to find a matching ViewData entry using the full expression name. If that fails, fall back to
            // ViewData.Model using the expression's relative name.
            var result = EvalComplexExpression(viewData, fullName);
            if (result == null)
            {
                if (string.IsNullOrEmpty(expression))
                {
                    // Null or empty expression name means current model even if that model is null.
                    result = new ViewDataInfo(container: viewData, value: viewData.Model);
                }
                else
                {
                    result = EvalComplexExpression(viewData.Model, expression);
                }
            }

            return result;
        }