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;
     }
    }
相关推荐
刀法如飞5 天前
一款Go语言Gin框架MVC脚手架,满足大部分场景
go·mvc·gin
莫寒清6 天前
Spring MVC:@PathVariable 注解详解
java·spring·mvc
莫寒清9 天前
Spring MVC:@RequestParam 注解详解
java·spring·mvc
莫寒清10 天前
Spring MVC:MultipartFile 详解
java·spring·mvc
*.✧屠苏隐遥(ノ◕ヮ◕)ノ*.✧10 天前
JSP, MVC, El, JSTL, MAC
java·开发语言·mvc·mac·jsp
fchampion11 天前
最终一致性
java·spring·rabbitmq·github·mvc
好学且牛逼的马14 天前
从“Servlet汪洋”到“响应式时代”:Spring MVC 1.x到5.x演进全记录与核心知识点详解
spring·servlet·mvc
知识即是力量ol17 天前
口语八股——Spring 面试实战指南(二):事务管理篇、Spring MVC 篇、Spring Boot 篇、Bean生命周期篇
spring·面试·mvc·springboot·八股·事务管理·bean生命周期
weixin_4219947817 天前
MVC 模式初探
mvc·.net·.netcore