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. go语言Notepad++简易开发环境搭建(windows)

    1.下载安装go语言:https://golang.org/dl/选择对应的平台,建议使用msi安装包,这个会帮你配置好环境变量(也许需要重启)对应的环境变量有: GOROOT - C:\Go\PAT ...

  2. JAVA FORK JOIN EXAMPLE--转

    http://www.javacreed.com/java-fork-join-example/ Java 7 introduced a new type of ExecutorService (Ja ...

  3. Android开发笔记(5)——方法调用(基础)

    转载请注明——博客园igoslly:http://www.cnblogs.com/igoslly/p/6833544.html   在实际方法调用中,程序按顺序逐句执行,直到“}”结束. 为避免程序大 ...

  4. 在PHP中调用php_ssh实现远程登陆linux服务器并执行shell脚本。

    这个功能主要用于在web端利用程序对远程服务器进行操作,通过PHP_ssh执行shell脚本来实现. 首先要安装php_ssh2组件,linux中centos7下有ssh2源,直接安装.window下 ...

  5. Build Tool-自动化构建工具

    输入:工程文件+编译说明文件: 处理:自动化构建工具+编译器: 输出:可执行文件. 相对于手动编译. Historically, build automation was accomplished t ...

  6. Operation Queues 面向对象的封装

    Operation Queues An operation queue is the Cocoa equivalent of a concurrent dispatch queue and is im ...

  7. PAT (Advanced Level) Practice(更新中)

    Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...

  8. [如何在mac下使用gulp] 2. gulp模块的常用方法

    常用的gulp模块方法有: gulp.src() gulp.src('client/one.js'); //指定明确的要处理文件 gulp.src('client/*.js'); //处理client ...

  9. soui edit passwrod模式下禁用输入法

    一直在用soui做客户端界面,今天发现密码edit在中文输入法下不能输入密码.我在想难道不是这样吗,密码就该用英文输入法啊. 然后我就用mfc的做了个demo,发现mfc的edit在密码模式下是可以用 ...

  10. UID中RUID、EUID和SUID的区别

    看UNIX相关的书时经常能遇到这几个概念,但一直没有好好去理清这几个概念,以致对这几个概念一直一知半解.今天好好区分了一下这几个概念并总结如下.说白了这几个UID引出都是为了系统的权限管理. 下面分别 ...