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. cocos creator学习

    2019-05-30 22:23:27 按照前一节我发的教程做,大概了解了Cocos creator的基本布局 但是你发现你不好写代码(感觉视频没有提) 需要下载VS code软件,在其上进行编辑,教 ...

  2. NHibernate学习(零)-本次学习遇到的错误汇总

    问题一: "System.TypeInitializationException"类型的未经处理的异常在 KimismeDemo.exe 中发生 其他信息: "NHibe ...

  3. WinForms_ListView中获取选中项数据值

    string value = listList.SelectedItems[0].SubItems[1].Text;//获取首行listview的值 string va = listList.Sele ...

  4. wait、notify、notifyAll实现线程间通信

    在Java中,可以通过配合调用Object对象的wait()方法和notify()方法或notifyAll()方法来实现线程间的通信.在线程中调用wait()方法,将阻塞等待其他线程的通知(其他线程调 ...

  5. ie9以下的浏览器兼容性问题

    .bind不兼容的问题Function.prototype.bind = function () { var fn = this, args = Array.prototype.slice.call( ...

  6. JS——筋斗云案例

    需求: 1.鼠标移动到哪里,云彩移动到哪里 2.鼠标离开,云彩回到原点 3.鼠标离开,云彩回到之前点击的地方 <!DOCTYPE html> <html lang="en& ...

  7. 6、scala Map和Tuple

    1.  创建Map 2.访问Map元素 3.修改Map元素的值 4.遍历Map 5.SortedMap和LinkedHashMap 6.Map的元素类型Tuple 1.  创建Map 创建不可变的Ma ...

  8. 10java内存

    java内存 1.栈---存储的是变量(不仅仅只有变量),不会对存储的内容进行赋值,存储的内容使用完成之后会立即进行清除 2.堆---存储的是对象.会对存储的内容进行赋值,存储内容使用完成之后会在某个 ...

  9. Java类及成员

    Java类及成员 类 类是对一类事物的的描述,是抽象的概念上的定义:类是创建对象的模板: public class TestClass { public static void main(String ...

  10. Django - 路由对应关系

    1.对url路由关系命名,以后可以根据此名称,生成自己想要的url urls.py中: url('^fdsaafdf(\d+)/',views.index,name='indexx') url('^f ...