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;
     }
    }
相关推荐
皮卡龙11 小时前
Spring MVC 接收请求参数的核心
java·spring·mvc
y1y1z2 天前
Spring MVC教程
java·spring·mvc
苏小瀚2 天前
[JavaEE] Spring Web MVC入门
前端·java-ee·mvc
Qiuner2 天前
Spring 机制六: MVC 全链路源码解析:从 DispatcherServlet 到返回值解析(超硬核源码深度)
java·spring boot·后端·spring·mvc
Lio n J3 天前
基于SpringBoot常用脱敏方案
spring boot·spring·mvc·状态模式
微祎_3 天前
Flutter 架构演进实战:从 MVC 到 Clean Architecture + Modular,打造可维护、可扩展、可测试的大型应用
flutter·架构·mvc
微祎_3 天前
Flutter 架构演进实战 2025:从 MVC 到 Clean Architecture + Modular,打造可维护、可扩展、可测试的大型应用
flutter·架构·mvc
⑩-5 天前
MVC-三层架构详解
java·架构·mvc
速易达网络5 天前
ASP.NET MVC 前后端商城系统介绍
后端·asp.net·mvc
m0_740043735 天前
Spring和SpringBoot和SpringMVC 的关系总结
java·spring boot·spring·mvc