A.创建Basic类型项目.

B.创建App_Code目录,在里面创建2个cshtml文件:

MyHelper.cshtml:

@helper MyTruncate(string input, int length)
{
<text>来自App_Code中的Helper->MyTruncate:</text>
if (input.Length <= length)
{
@input
}
else
{
@input.Substring(0, length) }
}
@helper MyAdd(int First, int Second)
{
int c = First + Second;
<i>来自App_Code中的Helper->MyAdd:</i>
@c.ToString() }

MyFunction.cshtml:

@functions{

   public static IHtmlString MyAdd4(int First,int Second)
{
int c;
c = First + Second;
return new HtmlString("<i>来自App_Code中的自己定义函数MyAdd4:</i>"+c.ToString());
} }

C.创建HomeController.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MvcCustomHelper.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/ public ActionResult Index()
{
return View();
}
public ActionResult InLineShow()
{ return View();
}
}
}

D.根文件夹下,新建一个MyHelpers文件夹,在里面,创建以下文件:

MyHtmlHelpers.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MvcCustomHelper.MyHelpers
{
public static class MyHtmlHelpers
{
public static IHtmlString MyTruncate2(this HtmlHelper helper, string input, int length)
{
if (input.Length <= length)
{ return new HtmlString("来自MyHelpers中Html扩展方法的MyTruncate2:" + input);
}
else
{
return new HtmlString("来自MyHelpers中Html扩展方法的MyTruncate2:" + input.Substring(0, length));
}
}
public static IHtmlString MyAdd2(this HtmlHelper helper, int First, int Second)
{
int c;
c = First + Second;
return new HtmlString("来自MyHelpers中Html扩展方法的MyAdd2:" + c.ToString());
}
}
}

E.View以下创建对应的View文件:

Index.cshtml:

@{
ViewBag.Title = "Index";
} <h1></h1>
@MyHelper.MyAdd(1,2)
<hr />
@MyHelper.MyTruncate("abcde",3)
<hr />
@MyFunction.MyAdd4(111,999)
<hr />
@using MvcCustomHelper.MyHelpers
@Html.MyTruncate2("bbbbbb", 4)
<hr />
@Html.MyAdd2(33, 55)
<hr />
@Html.ActionLink("内联方法和函数測试","InLineShow")

InLineShow.cshtml:

@{
ViewBag.Title = "Index2";
} <h2>内联帮助方法和函数測试</h2>
@helper MyTruncate3(string input, int length)
{
if (input.Length <= length)
{
<i>来自内联的Html扩展方法的MyTruncate3:</i>@input;
}
else
{
<i>来自内联的Html扩展方法的MyTruncate3:</i>@input.Substring(0, length)
}
}
@functions{
public IHtmlString MyAdd3(int First,int Second)
{
int c;
c = First + Second;
return new HtmlString("<i>来自内联的自己定义函数MyAdd3:</i>"+c.ToString());
}
}
<hr />
@MyTruncate3("vvvvvv",3)
<hr />
@MyAdd3(111,222)

AspNet MVC4 教学-27:Asp.Net MVC4 自己定义helper及function的高速Demo的更多相关文章

  1. AspNet MVC4 教学-22:Asp.Net MVC4 Partial View 技术高速应用Demo

    A.创建Basic类型的MVC项目. B.Model文件夹下,创建文件: LoginModel.cs: using System; using System.Collections.Generic; ...

  2. AspNet MVC4 教学-23:Asp.Net MVC4 Display And Editor 模板技术高速应用Demo

    A.创建Basic类型的项目. B.在Model文件夹下,创建3个文件: Role.cs: using System; using System.Collections.Generic; using ...

  3. AspNet MVC4 教育-28:Asp.Net MVC4 Ajax技术部门四舍五入余速Demo

    A.创建一个Basic项目类型. B.于Models创建一个文件夹: DivModel.cs: using System; using System.Collections.Generic; usin ...

  4. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(27)-权限管理系统-分配用户给角色

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(27)-权限管理系统-分配用户给角色 分配用户给角色,跟分配角色给用户操作是基本一致的. 打开模块维护,展 ...

  5. SignalR + KnockoutJS + ASP.NET MVC4 实现井字游戏

    1.1.1 摘要 今天,我们将使用SignalR + KnockoutJS + ASP.NET MVC实现一个实时HTML5的井字棋游戏. 首先,网络游戏平台一定要让用户登陆进来,所以需要一个登陆模块 ...

  6. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(1)-前言与目录(持续更新中...)

    转自:http://www.cnblogs.com/ymnets/p/3424309.html 曾几何时我想写一个系列的文章,但是由于工作很忙,一直没有时间更新博客.博客园园龄都1年了,却一直都是空空 ...

  7. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(33)-数据验证共享

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(33)-数据验证共享 注:本节阅读需要有MVC 自定义验证的基础,否则比较吃力 一直以来表单的验证都是不可 ...

  8. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(5)-EF增删改查by糟糕的代码

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(5)-EF增删改查by糟糕的代码 上一讲我们创建了一系列的解决方案,我们通过一个例子来看看层与层之间的关系 ...

  9. asp.net mvc4

    select省市联动选择城市 asp.net mvc4 2014-05-24 16:48 by P.C ++, 159 阅读, 2 评论, 收藏, 编辑 本文在 http://www.cnblogs. ...

随机推荐

  1. day24 03 多继承

    day24 03 多继承 正常的代码中  单继承==减少了代码的重复 继承表达的是一种 子类是父类的关系 1.简单的多继承关系 A,B,C,D四个类,其中D类继承A,B,C三个父类,因此也叫多继承,子 ...

  2. 345 Reverse Vowels of a String 反转字符串中的元音字母

    编写一个函数,以字符串作为输入,反转该字符串中的元音字母.示例 1:给定 s = "hello", 返回 "holle".示例 2:给定 s = "l ...

  3. [转]发布基于T4模板引擎的代码生成器[Kalman Studio]

    本文转自:http://www.cnblogs.com/lingyun_k/archive/2010/05/08/1730771.html 自己空闲时间写的一个代码生成器,基于T4模板引擎的,也不仅是 ...

  4. C# windform自定义控件的属性小知识

    word中的加粗变斜之类的一直让我以为是button,直到我接触了自定义控件,才发现实现这种机能最好的是CheckBox,然后我们在做一个系统的时候,这种控件有可能要用好多次,总不能在用一次的时候,就 ...

  5. JS——冒泡案例

    模态框 1.因为a链接和和顶级document都注册了单击事件,所以要阻止a链接向父级盒子冒泡,不然又会从document的单击事件走一遍 2.在document的单击事件中,只需要判断触发事件的目标 ...

  6. NetBeans将java项目编译成jar包

    1.找到file选项下的build.xml.

  7. JSP学习笔记 - 内置对象 Request

    1.主要掌握以下5个内置对象及其所属类,必须学会在java docs里根据类名查找相应的方法 request     javax.servlet.http.HttpServletRequest res ...

  8. rxswift-self.usernameTF.rx.text.orEmpty.map

    self.usernameTF.rx.text.orEmpty.map 一堆类型转化+数据处理的操作 self.usernameTF.rx:将textfiled用Reactive封装: .text:监 ...

  9. 扩增子图表解读3热图:差异菌、OTU及功能

    热图是使用颜色来展示数值矩阵的图形.通常还会结合行.列的聚类分析,以表达实验数据多方面的结果.  热图在生物学领域应用广泛,尤其在高通量测序的结果展示中很流行,如样品-基因表达,样品-OTU相对丰度矩 ...

  10. 文章或者观点说说等点赞功能实现(thinkphp)

    前端的代码: <!-- 点赞 --> <div class='btm'><a class='zan' id="{$article.id}" href= ...