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;
     }
    }
相关推荐
醇氧2 天前
MVC、MVP、MVVM 架构详解、分析与对比
架构·mvc
IKUN家族2 天前
Spring MVC(五)
java·spring·mvc
风起洛阳@不良使3 天前
Spring MVC的前端控制器配置(servlet+DispatcherServlet)
前端·spring·mvc
萧瑟余晖3 天前
Java深入解析篇十六之Spring MVC与WebFlux
java·spring·mvc
IKUN家族5 天前
Spring MVC(四)
java·spring·mvc
额恩6610 天前
神经网络入门:从基础到CNN全解析
mvc
周倦岚11 天前
ArkTS中的MVC、MVP、MVVM
华为·mvc·harmonyos
IKUN家族12 天前
Spring MVC(一)
java·spring·mvc
米码收割机13 天前
【SSM】Spring MVC_MyBatis SSM商城系统(源码+论文)【独一无二】
spring·mvc·mybatis
中微极客16 天前
神经形态计算:从忆阻器到SNN训练工程实践
人工智能·mvc