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;
     }
    }
相关推荐
Cyan_RA917 小时前
SpringMVC @RequestMapping的使用演示和细节 详解
java·开发语言·后端·spring·mvc·ssm·springmvc
周杰伦fans4 天前
.net core webapi/mvc阿里云服务器部署 - 错误解决
阿里云·mvc·.netcore
BFT白芙堂4 天前
GRASP 实验室研究 论文解读 | 机器人交互:基于神经网络引导变分推理的快速失配估计
人工智能·神经网络·机器学习·mvc·人机交互·科研教育机器人·具身智能平台
William_cl4 天前
MVC 中 AJAX 与前后端交互深度实战(含独家避坑与场景化方案)
ajax·mvc·交互
Cyan_RA95 天前
SpringMVC 执行流程分析 详解(图解SpringMVC执行流程)
java·人工智能·后端·spring·mvc·ssm·springmvc
hello 早上好6 天前
Spring MVC 类型转换与参数绑定:从架构到实战
spring·架构·mvc
sanggou6 天前
License 集成 Spring Gateway:解决 WebFlux 非阻塞与 Spring MVC Servlet 阻塞兼容问题
spring·gateway·mvc
optimistic_chen6 天前
【Java EE进阶 --- SpringBoot】Spring DI详解
spring boot·笔记·后端·spring·java-ee·mvc·di
速易达网络6 天前
ASP.NET MVC 连接 MySQL 数据库查询示例
数据库·asp.net·mvc
cyforkk7 天前
Spring 异常处理器:从混乱到有序,优雅处理所有异常
java·后端·spring·mvc