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;
     }
    }
相关推荐
日日行不惧千万里6 小时前
MVC、MVP、MVVM详解
mvc
好好研究7 小时前
SpringMVC框架 - 文件上传
java·spring·mvc·idea
好好研究12 小时前
SpringMVC框架 - 获取请求参数常用的注解
java·spring·mvc
想不明白的过度思考者13 小时前
Spring Web MVC从入门到实战
java·前端·spring·mvc
黄昏恋慕黎明2 天前
spring MVC了解
java·后端·spring·mvc
酷柚易汛智推官3 天前
当知识图谱遇上RAG:LightRAG如何重塑信息检索新范
mvc·酷柚易汛
William_cl4 天前
【ASP.NET进阶】Controller 层基础:从 MVC 5 到 Core,继承的奥秘与避坑指南
后端·asp.net·mvc
百花~4 天前
Spring Web MVC~
前端·spring·mvc
吴名氏.7 天前
电子书《ASP.NET MVC企业级实战》
后端·asp.net·mvc·编程语言
凌波粒8 天前
SpringMVC基础教程(1)--MVC/DispathcerServlet
java·spring·mvc