ASP.NET MVC(二) HtmlHelper







强类型





》》》 Form

Html.Action() 执行一个Action,并返回html字符串。

Html.ActionLink() 生成一个超链接。

》》》 htmlhelper 扩展方法

/// 扩展方法 三要素

  1. 静态类
  2. 静态方法
  3. this

》》》》上面需要引入命名空间,

》》》 不需要引入命名空间

csharp 复制代码
    public static class CustomHelpers
    {
     //Submit Button Helper
     public static MvcHtmlString SubmitButton(this HtmlHelper helper, string 
     buttonText)
     {
     string str = "<input type=\"submit\" value=\"" + buttonText + "\" />";
     return new MvcHtmlString(str);
     }
     public static string Mylable(this HtmlHelper helper,string txt)
     {
            //  这个方法返回到html中存在编码问题。
            return string.Format("<span>{0}</span>",txt);
    }
     //Readonly Strongly-Typed TextBox Helper
     public static MvcHtmlString TextBoxFor<TModel, TValue>(this 
     HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>>
     expression, bool isReadonly)
     {
     MvcHtmlString html = default(MvcHtmlString);
     
     if (isReadonly)
     {
     html = System.Web.Mvc.Html.InputExtensions.TextBoxFor(htmlHelper,
     expression, new { @class = "readOnly",
     @readonly = "read-only" });
     }
     else
     {
     html = System.Web.Mvc.Html.InputExtensions.TextBoxFor(htmlHelper,
     expression);
     }
     return html;
     }
    }
相关推荐
笛卡尔的心跳1 小时前
Spring MVC 注解
java·spring·mvc
小松加哲13 小时前
Spring MVC 核心原理全解析
java·spring·mvc
那个失眠的夜1 天前
RESTful 语法规范 核心注解详解
java·spring·mvc·mybatis
羌俊恩1 天前
Centos环境django项目部署过程
django·flask·centos·mvc·mtv·web项目框架
Foreer黑爷3 天前
Spring MVC原理与源码:从请求到响应的全流程解析
java·spring·mvc
曹牧4 天前
Spring MVC中使用HttpServletRequest和HttpServletResponse
java·spring·mvc
曹牧4 天前
Spring MVC配置文件
java·spring·mvc
CPUOS20104 天前
嵌入式C语言高级编程之MVC设计模式
c语言·设计模式·mvc
xxjj998a5 天前
【Spring】Spring MVC案例
java·spring·mvc
医疗信息化王工9 天前
基于ASP.NET Core的医院输血审核系统设计与实现
后端·mvc·asp.net core·输血审核