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;
     }
    }
相关推荐
wang09072 天前
自己动手写一个spring之MVC_3
java·spring·mvc
shepherd1262 天前
一次把 Spring MVC 文件上传参数“查没了”的排查:multipart、Filter 与 request body 的连环坑
java·后端·spring·mvc
阿pin2 天前
Android随笔-MVC/MVP/MVVM/MVI
android·mvc·mvvm·mvp·mvi
李燚4 天前
Go 项目怎么组织:DDD 4 层 vs MVC vs 脚本式
开发语言·golang·mvc·ddd·agent框架·eino
帅气的冬天5 天前
在ASP.NET MVC中对表进行通用的增删改
后端·asp.net·mvc
正儿八经的少年6 天前
Spring MVC 框架
java·spring·mvc
Evand J8 天前
【MATLAB例程】一维非线性状态滤波对比:经典EKF、MCC-EKF、MVC-EKF。在含异常测量的场景下,对比三种滤波方法的估计曲线和误差统计。
matlab·mvc·ekf·滤波·mcc
任性的芝麻8 天前
ASP.NET MVC 中的异步方式
后端·asp.net·mvc
任性的自行车8 天前
ASP.NET MVC 4路线图
后端·asp.net·mvc
zzb15801 个月前
ios基础-MVC-UIView
ios·mvc·cocoa