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;
     }
    }
相关推荐
MSTcheng.7 小时前
探索昇腾底层逻辑:从ops-nn视角解读aclnn两阶段设计理念
mvc
勤奋的小王同学~1 天前
SpringMVC
java·spring·mvc
IT 行者3 天前
Spring MVC 慎用@InitBinder,谨防内存泄漏
java·spring·mvc
树码小子3 天前
SpringMVC(6) :阶段总结
spring·mvc
风景的人生4 天前
application/x-www-form-urlencoded
java·mvc
树码小子4 天前
SpringMVC(12)综合案例练习:图书管理系统(后续仍会使用)
spring·mvc
码云数智-园园5 天前
从 ASP.NET MVC 到现代 .NET:一份完整的迁移升级指南
asp.net·mvc·.net
lfq7612045 天前
.NET Framework 下 C# MVC 项目敏感信息安全存储方法
安全·c#·mvc·.net
灵感菇_5 天前
详细解析 MVC/MVP/MVVM/MVI 架构
架构·mvc·mvvm·mvp·mvi
树码小子5 天前
SpringMCV(9)响应:返回静态页面 & 修改响应数据
spring·mvc