参考.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来扩展实现下