Esempio n. 1
0
        public static string GetIndexerFieldName(this TemplateInfo templateInfo)
        {
            string fieldName = templateInfo.GetFullHtmlFieldName("Index");

            fieldName = _stripIndexerRegex.Replace(fieldName, string.Empty);
            if (fieldName.StartsWith("."))
            {
                fieldName = fieldName.Substring(1);
            }
            return(fieldName);
        }
Esempio n. 2
0
        public static int GetIndex(this TemplateInfo templateInfo)
        {
            string fieldName = templateInfo.GetFullHtmlFieldName("Index");
            var    match     = _stripIndexerRegex.Match(fieldName);

            if (match.Success)
            {
                return(int.Parse(match.Groups["index"].Value));
            }
            return(0);
        }
Esempio n. 3
0
        public static HtmlHelper <TModel> HtmlHelperFor <TModel>(this HtmlHelper htmlHelper, TModel model, string htmlFieldPrefix)
        {
            var viewDataContainer = CreateViewDataContainer(htmlHelper.ViewData, model);

            TemplateInfo templateInfo = viewDataContainer.ViewData.TemplateInfo;

            if (!String.IsNullOrEmpty(htmlFieldPrefix))
            {
                templateInfo.HtmlFieldPrefix = templateInfo.GetFullHtmlFieldName(htmlFieldPrefix);
            }

            ViewContext viewContext    = htmlHelper.ViewContext;
            ViewContext newViewContext = new ViewContext(viewContext.Controller.ControllerContext, viewContext.View, viewDataContainer.ViewData, viewContext.TempData, viewContext.Writer);

            return(new HtmlHelper <TModel>(newViewContext, viewDataContainer, htmlHelper.RouteCollection));
        }
Esempio n. 4
0
        public static HtmlHelper <TModel> ForModel <TModel>(
            HtmlHelper currentHtml,
            TModel model,
            string?htmlFieldPrefix = null)
        {
            if (currentHtml is null)
            {
                throw new ArgumentNullException(nameof(currentHtml));
            }

            ViewDataDictionary currentViewData = currentHtml.ViewData;

            // Cannot call new ViewDataDictionary<TModel>(currentViewData)
            // because currentViewData.Model might be incompatible with TModel

            var tempDictionary = new ViewDataDictionary(currentViewData)
            {
                Model = model
            };

            var container = new ViewDataContainer {
                ViewData = new ViewDataDictionary <TModel>(tempDictionary)
                {
                    // setting new TemplateInfo clears VisitedObjects cache
                    TemplateInfo = new TemplateInfo {
                        HtmlFieldPrefix = currentViewData.TemplateInfo.HtmlFieldPrefix
                    }
                }
            };

            if (!String.IsNullOrEmpty(htmlFieldPrefix))
            {
                TemplateInfo templateInfo = container.ViewData.TemplateInfo;
                templateInfo.HtmlFieldPrefix = templateInfo.GetFullHtmlFieldName(htmlFieldPrefix);
            }

            // new ViewContext resets FormContext
            ViewContext newViewContext = currentHtml.ViewContext.Clone(viewData: container.ViewData);

            return(currentHtml.Clone <TModel>(newViewContext, container));
        }
Esempio n. 5
0
 public static string GetFullHtmlFieldName <TModel, TProperty>(this TemplateInfo templateInfo, Expression <Func <TModel, TProperty> > expression)
 {
     return(templateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression)));
 }
Esempio n. 6
0
 public static string GetFullArrayHtmlFieldName(this TemplateInfo templateInfo, int index, string propertyName)
 {
     return(templateInfo.GetFullHtmlFieldName("") + "[" + index + "]" + "." + propertyName);
 }
Esempio n. 7
0
 private string GetName <TPoperty>(Expression <Func <TModel, TPoperty> > expression)
 {
     return(_info.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression)));
 }