Exemplo 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);
        }
Exemplo 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);
        }
Exemplo 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));
        }
Exemplo 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));
        }
Exemplo n.º 5
0
 public static string GetFullHtmlFieldName <TModel, TProperty>(this TemplateInfo templateInfo, Expression <Func <TModel, TProperty> > expression)
 {
     return(templateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression)));
 }
Exemplo n.º 6
0
 public static string GetFullArrayHtmlFieldName(this TemplateInfo templateInfo, int index, string propertyName)
 {
     return(templateInfo.GetFullHtmlFieldName("") + "[" + index + "]" + "." + propertyName);
 }
Exemplo n.º 7
0
 private string GetName <TPoperty>(Expression <Func <TModel, TPoperty> > expression)
 {
     return(_info.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression)));
 }