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;
     }
    }
相关推荐
PythonicCC2 天前
Django中的MVC和MVT模式
数据库·django·mvc
神仙别闹5 天前
基于 JSP+Mysql实现MVC房屋租赁系统
java·mysql·mvc
optimistic_chen9 天前
【Java EE进阶 --- SpringBoot】初识Spring(创建SpringBoot项目)
spring boot·后端·spring·java-ee·tomcat·mvc·idea
不知名搬运工10 天前
9 ABP Framework 中的 MVC 和 Razor Pages
mvc
周杰伦fans12 天前
.NET Core MVC中CSHTML
mvc·.netcore
右手嘚温暖13 天前
SpringMvc的原理深度剖析及源码解读
spring·开源·mvc
Alt.914 天前
SpringMVC(二)
java·开发语言·mvc
ysy164806723916 天前
Spring、Spring MVC、MyBatis 和 Spring Boot的关系
spring·mvc·mybatis
探索java17 天前
Spring MVC框架中全局异常处理机制详解
java·spring·mvc
可曾去过倒悬山18 天前
SpringMvc跨域配置方法详解
spring·mvc