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. flask中路由系统

    flask中的路由我们并不陌生,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST"]) 1 ...

  2. [转]linux sudo 命令

    转自:http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/11/11/2245341.html “Sudo”是Unix/Linux平台上的一个 ...

  3. input获得焦点和失去焦点

    总结:placeholder因为在IE7 8 9 浏览器不支持所以没用它效果:当input获取光标的时候如果是默认提示则input内容为空.如果不是则为输入内容           当失去光标的时候, ...

  4. win32绘图基础

     获取设备环境句柄: (1)WM_PAINT消息中: PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd,&ps); EndPaint(hwnd,&ps ...

  5. (转) 分布式文件存储FastDFS(七)FastDFS配置文件详解

    http://blog.csdn.net/xingjiarong/article/details/50752586 配置FastDFS时,修改配置文件是很重要的一个步骤,理解配置文件中每一项的意义更加 ...

  6. CorelDRAW快速制作抖音幻影图像效果

    本教程讲解非常受欢迎的幻影图像效果(Anaglyph 3d),也叫图像分色立体效果,这其中我们要用到CorelDRAW中的透明度工具. 在开始实施Anaglyph效应之前,应当知道,Anaglyph  ...

  7. js常用方法和技巧

    随着AJAX的流行,js又得到了很多人的重视,js最大的优势就是它能够对html上的所有元素进行操作,包括创建标签元素,更改元素属性等,这样就使得我们能够利用js来实现很多的动态效果,来提供给用户更强 ...

  8. bootstrapValidator代码中开启验证和判断验证是否通过

    //开启验证 $('#saveadmin_form').data('bootstrapValidator').validate(); //是否通过校验 if(!$('#saveadmin_form') ...

  9. Python学习【第4篇】:Python之文件操作

    文件操作 读取一行 f=open("D:\\1.txt",'rb') print f.readline() f.close() 将文件内容保存在一个list with open(& ...

  10. (C/C++学习)8.C++ Lambda

    一.生成随机数字 假设我们有一个vector<int>容器,想用100以内的随机数初始化它,其中一个办法是通过generate函数生成,如代码1所示.generate函数接受三个参数,前两 ...