예제 #1
0
        /// <summary>
        /// Renders the specified React component, along with its client-side initialisation code.
        /// Normally you would use the <see cref="React{T}"/> method, but <see cref="ReactWithInit{T}"/>
        /// is useful when rendering self-contained partial views.
        /// </summary>
        /// <typeparam name="T">Type of the props</typeparam>
        /// <param name="htmlHelper">HTML helper</param>
        /// <param name="componentName">Name of the component</param>
        /// <param name="props">Props to initialise the component with</param>
        /// <param name="htmlTag">HTML tag to wrap the component in. Defaults to &lt;div&gt;</param>
        /// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
        /// <param name="clientOnly">Skip rendering server-side and only output client-side initialisation code. Defaults to <c>false</c></param>
        /// <param name="containerClass">HTML class(es) to set on the container tag</param>
        /// <returns>The component's HTML</returns>
        public static IHtmlString ReactWithInit <T>(
            this IHtmlHelper htmlHelper,
            string componentName,
            T props,
            string htmlTag        = null,
            string containerId    = null,
            bool clientOnly       = false,
            string containerClass = null
            )
        {
            var reactComponent = Environment.CreateComponent(componentName, props, containerId, clientOnly);

            if (!string.IsNullOrEmpty(htmlTag))
            {
                reactComponent.ContainerTag = htmlTag;
            }
            if (!string.IsNullOrEmpty(containerClass))
            {
                reactComponent.ContainerClass = containerClass;
            }
            var html   = reactComponent.RenderHtml(clientOnly);
            var script = new TagBuilder("script")
            {
                InnerHtml = reactComponent.RenderJavaScript()
            };

            return(new HtmlString(html + System.Environment.NewLine + script.ToString()));
        }
예제 #2
0
        /// <summary>
        /// Renders the specified React component
        /// </summary>
        /// <typeparam name="T">Type of the props</typeparam>
        /// <param name="htmlHelper">HTML helper</param>
        /// <param name="componentName">Name of the component</param>
        /// <param name="props">Props to initialise the component with</param>
        /// <param name="htmlTag">HTML tag to wrap the component in. Defaults to &lt;div&gt;</param>
        /// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
        /// <param name="clientOnly">Skip rendering server-side and only output client-side initialisation code. Defaults to <c>false</c></param>
        /// <param name="serverOnly">Skip rendering React specific data-attributes during server side rendering. Defaults to <c>false</c></param>
        /// <param name="containerClass">HTML class(es) to set on the container tag</param>
        /// <returns>The component's HTML</returns>
        public static MvcHtmlString React <T>(
            this IHtmlHelper htmlHelper,
            string componentName,
            T props,
            string htmlTag        = null,
            string containerId    = null,
            bool clientOnly       = false,
            bool serverOnly       = false,
            string containerClass = null
            )
        {
            var reactComponent = Environment.CreateComponent(componentName, props, containerId, clientOnly);

            if (!string.IsNullOrEmpty(htmlTag))
            {
                reactComponent.ContainerTag = htmlTag;
            }
            if (!string.IsNullOrEmpty(containerClass))
            {
                reactComponent.ContainerClass = containerClass;
            }
            var result = reactComponent.RenderHtml(clientOnly, serverOnly);

            return(new MvcHtmlString(result));
        }
예제 #3
0
        /// <summary>
        /// Renders the JavaScript required to initialise all components client-side. This will
        /// attach event handlers to the server-rendered HTML.
        /// </summary>
        /// <returns>JavaScript for all components</returns>
        public static IHtmlString ReactInitJavaScript(this IHtmlHelper htmlHelper, bool clientOnly = false)
        {
            var script = Environment.GetInitJavaScript(clientOnly);
            var tag    = new TagBuilder("script")
            {
                InnerHtml = script
            };

            return(new HtmlString(tag.ToString()));
        }
 public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable <SelectListItem> selectList, string optionLabel, IDictionary <string, object> htmlAttributes)
 {
     return(DropDownListHelper(htmlHelper, metadata: null, expression: name, selectList: selectList, optionLabel: optionLabel, htmlAttributes: htmlAttributes));
 }
 public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable <SelectListItem> selectList, string optionLabel, object htmlAttributes)
 {
     return(DropDownList(htmlHelper, name, selectList, optionLabel, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)));
 }
 public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable <SelectListItem> selectList, string optionLabel)
 {
     return(DropDownList(htmlHelper, name, selectList, optionLabel, null /* htmlAttributes */));
 }
예제 #7
0
 public static MvcHtmlString LabelFor <TModel, TValue>(this HtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression)
 {
     return(LabelFor(html, expression, null));
 }
예제 #8
0
 public static MvcHtmlString LabelForModel(this HtmlHelper html, string labelText)
 {
     return(LabelHelper(html, html.ViewData.ModelMetadata, String.Empty, labelText));
 }
예제 #9
0
		public void SetUp()
		{
			html = new HtmlHelper<Person>();
		}
예제 #10
0
 public static MvcHtmlString DropDownListFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, IEnumerable <SelectListItem> selectList, string optionLabel, object htmlAttributes)
 {
     return(DropDownListFor(htmlHelper, expression, selectList, optionLabel, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)));
 }
예제 #11
0
 public static MvcHtmlString ListBoxFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, IEnumerable <SelectListItem> selectList)
 {
     return(ListBoxFor(htmlHelper, expression, selectList, null /* htmlAttributes */));
 }
예제 #12
0
 public static MvcHtmlString ListBox(this HtmlHelper htmlHelper, string name, IEnumerable <SelectListItem> selectList, IDictionary <string, object> htmlAttributes)
 {
     return(ListBoxHelper(htmlHelper, metadata: null, name: name, selectList: selectList, htmlAttributes: htmlAttributes));
 }
예제 #13
0
 public static MvcHtmlString ListBox(this HtmlHelper htmlHelper, string name, IEnumerable <SelectListItem> selectList, object htmlAttributes)
 {
     return(ListBox(htmlHelper, name, selectList, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)));
 }
예제 #14
0
 public static MvcHtmlString ListBox(this HtmlHelper htmlHelper, string name, IEnumerable <SelectListItem> selectList)
 {
     return(ListBox(htmlHelper, name, selectList, (IDictionary <string, object>)null));
 }
예제 #15
0
        // ListBox

        public static MvcHtmlString ListBox(this HtmlHelper htmlHelper, string name)
        {
            return(ListBox(htmlHelper, name, null /* selectList */, null /* htmlAttributes */));
        }
예제 #16
0
 public static IRequest GetHttpRequest(this HtmlHelper html)
 {
     return(html?.HttpRequest);
 }
예제 #17
0
 public static MvcHtmlString DropDownListFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, IEnumerable <SelectListItem> selectList, IDictionary <string, object> htmlAttributes)
 {
     return(DropDownListFor(htmlHelper, expression, selectList, null /* optionLabel */, htmlAttributes));
 }
예제 #18
0
 public static MvcHtmlString DropDownListFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, IEnumerable <SelectListItem> selectList, string optionLabel)
 {
     return(DropDownListFor(htmlHelper, expression, selectList, optionLabel, null /* htmlAttributes */));
 }
예제 #19
0
 private static MvcHtmlString ListBoxHelper(HtmlHelper htmlHelper, ModelMetadata metadata, string name, IEnumerable <SelectListItem> selectList, IDictionary <string, object> htmlAttributes)
 {
     return(SelectInternal(htmlHelper, metadata, optionLabel: null, name: name, selectList: selectList, allowMultiple: true, htmlAttributes: htmlAttributes));
 }
예제 #20
0
 private static MvcHtmlString DropDownListHelper(HtmlHelper htmlHelper, ModelMetadata metadata, string expression, IEnumerable <SelectListItem> selectList, string optionLabel, IDictionary <string, object> htmlAttributes)
 {
     return(SelectInternal(htmlHelper, metadata, optionLabel, expression, selectList, allowMultiple: false, htmlAttributes: htmlAttributes));
 }
예제 #21
0
 public static MvcHtmlString TitleCase(this ServiceStack.Html.HtmlHelper helper, string text)
 {
     return(MvcHtmlString.Create(Regex.Replace(text, "([A-Z]{1,2}|[0-9]+)", " $1").TrimStart()));
 }
예제 #22
0
        private static MvcHtmlString SelectInternal(this HtmlHelper htmlHelper, ModelMetadata metadata, string optionLabel, string name, IEnumerable <SelectListItem> selectList, bool allowMultiple, IDictionary <string, object> htmlAttributes)
        {
            //string fullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
            string fullName = name;

            if (String.IsNullOrEmpty(fullName))
            {
                throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name");
            }

            bool usedViewData = false;

            // If we got a null selectList, try to use ViewData to get the list of items.
            if (selectList == null)
            {
                selectList   = htmlHelper.GetSelectData(name);
                usedViewData = true;
            }

            object defaultValue = (allowMultiple) ? htmlHelper.GetModelStateValue(fullName, typeof(string[])) : htmlHelper.GetModelStateValue(fullName, typeof(string));

            // If we haven't already used ViewData to get the entire list of items then we need to
            // use the ViewData-supplied value before using the parameter-supplied value.
            if (!usedViewData && defaultValue == null && !String.IsNullOrEmpty(name))
            {
                defaultValue = htmlHelper.ViewData.Eval(name);
            }

            if (defaultValue != null)
            {
                selectList = GetSelectListWithDefaultValue(selectList, defaultValue, allowMultiple);
            }

            // Convert each ListItem to an <option> tag
            StringBuilder listItemBuilder = StringBuilderCache.Allocate();

            // Make optionLabel the first item that gets rendered.
            if (optionLabel != null)
            {
                listItemBuilder.AppendLine(ListItemToOption(new SelectListItem()
                {
                    Text = optionLabel, Value = String.Empty, Selected = false
                }));
            }

            foreach (SelectListItem item in selectList)
            {
                listItemBuilder.AppendLine(ListItemToOption(item));
            }

            TagBuilder tagBuilder = new TagBuilder("select")
            {
                InnerHtml = StringBuilderCache.Retrieve(listItemBuilder)
            };

            tagBuilder.MergeAttributes(htmlAttributes);
            tagBuilder.MergeAttribute("name", fullName, true /* replaceExisting */);
            tagBuilder.GenerateId(fullName);
            if (allowMultiple)
            {
                tagBuilder.MergeAttribute("multiple", "multiple");
            }

            // If there are any errors for a named field, we add the css attribute.
            ModelState modelState;

            if (htmlHelper.ViewData.ModelState.TryGetValue(fullName, out modelState))
            {
                if (modelState.Errors.Count > 0)
                {
                    tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
                }
            }

            tagBuilder.MergeAttributes(htmlHelper.GetUnobtrusiveValidationAttributes(name, metadata));

            return(tagBuilder.ToMvcHtmlString(TagRenderMode.Normal));
        }
예제 #23
0
 public static MvcHtmlString LabelForModel(this HtmlHelper html)
 {
     return(LabelForModel(html, null));
 }
예제 #24
0
 public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, string optionLabel)
 {
     return(DropDownList(htmlHelper, name, null /* selectList */, optionLabel, null /* htmlAttributes */));
 }
예제 #25
0
 public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable <SelectListItem> selectList, IDictionary <string, object> htmlAttributes)
 {
     return(DropDownList(htmlHelper, name, selectList, null /* optionLabel */, htmlAttributes));
 }
예제 #26
0
        public static MvcHtmlString DataTableAjaxUrl(this ServiceStack.Html.HtmlHelper helper, string ajaxBaseUri)
        {
            var url = string.Format("{0}{1}", ajaxBaseUri, HttpUtility.UrlDecode(new Uri(helper.HttpRequest.AbsoluteUri).Query));

            return(MvcHtmlString.Create(url));
        }
예제 #27
0
 public static MvcHtmlString Label(this HtmlHelper html, string expression)
 {
     return(Label(html, expression, null));
 }