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;
     }
    }
相关推荐
武帝为此1 天前
【SpringMVC 入门介绍】
java·spring·mvc
Cyanto2 天前
Spring MVC 核心枢纽:DispatcherServlet 的深度解析与实践价值
java·spring·mvc
fuze23332 天前
Spring MVC扩展消息转换器-->格式化时间信息
java·spring·mvc
qq_334060212 天前
springmvc
java·spring·mvc
草履虫建模3 天前
Tomcat 和 Spring MVC
java·spring boot·spring·spring cloud·tomcat·mvc·intellij-idea
小白的代码日记4 天前
Spring MVC 入门案例:从代码到原理的深度剖析
java·spring·mvc
三目君5 天前
SpringMVC异步处理Servlet
java·spring·servlet·tomcat·mvc
fie88895 天前
Spring MVC扩展与SSM框架整合
java·spring·mvc
我叫小白菜6 天前
【Java_EE】Spring MVC
java·spring·mvc
yuren_xia7 天前
Spring MVC执行流程简介
java·spring·mvc