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;
     }
    }
相关推荐
wang_shu_mo_ran4 天前
Spring MVC的常用注解和用法(一)
java·spring·mvc
Huanzhi_Lin8 天前
UI业务模块化随笔
ui·mvc·mvp·ecs·ui框架·模块化开发
杨运交9 天前
[070][Web模块]Spring MVC TOTP 二次认证拦截器:设计与源码深度解析
前端·spring·mvc
爱敲代码的小杨.12 天前
【Spring】Spring Web MVC
前端·spring·mvc
风云14 天前
Vane.Dispatch 1.0.0 发布:一个与容器、传输层零耦合的 .NET 服务分发引擎
微服务·mvc·.net·ndf·vane.dispatch
事圆则缓15 天前
MVC、MVP、MVVM、MVI 的区别与实现原理
android·kotlin·mvc
Zzzzmo_16 天前
Spring MVC
java·spring·mvc·cookie/session
Mr.敦的私房菜16 天前
【SpringBoot请求记录】Spring Boot Actuator 自定义记录 HTTP 服务请求
spring boot·mvc·运维开发
MetaLite24 天前
Spring-MVC微服务接口怎么分层-网关Controller与Service边界
spring·微服务·mvc
李昊哲小课1 个月前
SpringMVC 完整执行流程
spring boot·spring·mvc