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;
     }
    }
相关推荐
一水鉴天12 小时前
专题讨论 类型理论和范畴理论之间的关系 之2 整体设计中的“闭” 解题和“位”问题 (ima.copilot)
线性代数·矩阵·mvc
青衫码上行21 小时前
【JavaWeb学习 | 第18篇】Servlet与MVC
java·学习·servlet·mvc
库库林_沙琪马3 天前
3、MVC的支持
mvc
低客的黑调3 天前
Spring MVC 全面详解:原理、组件、实战与高级特性
java·spring·mvc
STARFALL0013 天前
spring mvc 自定义Converter 设置
java·spring·mvc
哈哈~haha4 天前
ui5_Walkthrough_Step 7:JSON Model
json·mvc·module·ui5
好好研究6 天前
SSM整合(一)
java·spring·mvc·mybatis·db
凌波粒6 天前
Springboot基础教程(4)--Thymeleaf模板引擎/MVC自动装配原理
spring boot·后端·mvc
老神在在0016 天前
MyBatis02
后端·spring·java-ee·mvc·mybatis
就叫飞六吧7 天前
Spring MVC 接口命名为什么会有 *.do/actions等身影?
java·spring·mvc