HtmlHelper扩展

参考.Net Core源码HtmlHelperInputExtensions对EasyUI的样式中的TextBox自定义成了HtmlHelper的扩展方法便于在Razor页面中快速使用

using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;

namespace MyMis.MvcExtend.MvcHelper
{
    public static class CustomerHtmlHelper
    {
        public static IHtmlContent DateTextBox(this HtmlHelper html, string id)
        {
            TagBuilder tag = new TagBuilder("date");
            //tag.MergeAttribute("id", id);
            //tag.MergeAttribute("name", id);
            //tag.MergeAttribute("class", "easyui-my97\" data-options=\"dateFmt: 'yyyy-MM-dd'");

            string sHtml = "<input id='" + id + "' name='" + id + "'  class=\"easyui-my97\" data-options=\"dateFmt: 'yyyy-MM-dd'\" > ";
            tag.InnerHtml.Append(sHtml);
            return new HtmlString(tag.ToString());
        }
        /// <summary>
        /// EasyUi TextBox格式
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static IHtmlContent EasyTextBox<TModel, TResult>(this IHtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TResult>> expression, string width = "80%", string height = "25px")
        {
            if (htmlHelper == null)
            {
                throw new ArgumentNullException(nameof(htmlHelper));
            }

            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            string style = $"width:{width};height:{height};";

            TagBuilder tag = new TagBuilder("text");
            return htmlHelper.TextBoxFor(expression, new { @class = "easyui-textbox", @style = style });
        }
        /// <summary>
        /// EasyUi TextBox格式 Required
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static IHtmlContent EasyRequiredTextBox<TModel, TResult>(this IHtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TResult>> expression,string width="80%",string height="25px")
        {
            if (htmlHelper == null)
            {
                throw new ArgumentNullException(nameof(htmlHelper));
            }

            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            string style = $"width:{width};height:{height};";

            TagBuilder tag = new TagBuilder("text");
            return htmlHelper.TextBoxFor(expression, new { @class = "easyui-textbox", @style = style, data_options = "required:true" });
        }
        /// <summary>
        /// EasyUi TextBox格式 只读
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static IHtmlContent EasyReadOnlyTextBox<TModel, TResult>(this IHtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TResult>> expression, string width = "80%", string height = "25px")
        {
            if (htmlHelper == null)
            {
                throw new ArgumentNullException(nameof(htmlHelper));
            }

            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            string style = $"width:{width};height:{height};";

            TagBuilder tag = new TagBuilder("text");
            return htmlHelper.TextBoxFor(expression, new { @class = "easyui-textbox", @style = style, data_options = "editable:false" });
        }
    }
}

使用时页面需引入命名空间 @using MyMis.MvcExtend.MvcHelper;

这样就可以使用了

@Html.EasyReadOnlyTextBox(b => b.customerid)

以上只是加了EasyUi中简单的TextBox的样式,之后有时间把EasyUI中的大部分控件都用HtmlHelper来扩展实现下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值