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;
     }
    }
相关推荐
m0_748248027 小时前
Spring MVC中@RequestMapping注解的全面解析
java·spring·mvc
Kay_Liang8 小时前
Spring中@Controller与@RestController核心解析
java·开发语言·spring boot·后端·spring·mvc·注解
行思理8 小时前
Spring MVC 注释新手教程
java·spring·mvc
William_cl10 小时前
【ASP.NET MVC 进阶】DataAnnotations 特性验证全解析:从基础到避坑,让数据校验像 “安检“ 一样靠谱
后端·asp.net·mvc
William_cl12 小时前
C# ASP.NET MVC Model 分类:数据传输对象(DTO)—— 跨层传数的 “精简快递“
c#·asp.net·mvc
William_cl4 天前
拆解ASP.NET MVC 视图模型:为 View 量身定制的 “数据小票“
后端·asp.net·mvc
lang201509285 天前
Spring MVC配置全解析
java·spring·mvc
William_cl6 天前
从 MVC 5 到 Core MVC:ASP.NET MVC 框架的 “进化之路“
后端·asp.net·mvc
Filotimo_6 天前
Spring MVC 数据校验
java·spring·mvc
♡喜欢做梦8 天前
Spring Web MVC 入门秘籍:从概念到实践的快速通道(上)
前端·spring·mvc