AspNet MVC4 教学-27:Asp.Net MVC4 自己定义helper及function的高速Demo
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的更多相关文章
- AspNet MVC4 教学-22:Asp.Net MVC4 Partial View 技术高速应用Demo
A.创建Basic类型的MVC项目. B.Model文件夹下,创建文件: LoginModel.cs: using System; using System.Collections.Generic; ...
- AspNet MVC4 教学-23:Asp.Net MVC4 Display And Editor 模板技术高速应用Demo
A.创建Basic类型的项目. B.在Model文件夹下,创建3个文件: Role.cs: using System; using System.Collections.Generic; using ...
- AspNet MVC4 教育-28:Asp.Net MVC4 Ajax技术部门四舍五入余速Demo
A.创建一个Basic项目类型. B.于Models创建一个文件夹: DivModel.cs: using System; using System.Collections.Generic; usin ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(27)-权限管理系统-分配用户给角色
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(27)-权限管理系统-分配用户给角色 分配用户给角色,跟分配角色给用户操作是基本一致的. 打开模块维护,展 ...
- SignalR + KnockoutJS + ASP.NET MVC4 实现井字游戏
1.1.1 摘要 今天,我们将使用SignalR + KnockoutJS + ASP.NET MVC实现一个实时HTML5的井字棋游戏. 首先,网络游戏平台一定要让用户登陆进来,所以需要一个登陆模块 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(1)-前言与目录(持续更新中...)
转自:http://www.cnblogs.com/ymnets/p/3424309.html 曾几何时我想写一个系列的文章,但是由于工作很忙,一直没有时间更新博客.博客园园龄都1年了,却一直都是空空 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(33)-数据验证共享
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(33)-数据验证共享 注:本节阅读需要有MVC 自定义验证的基础,否则比较吃力 一直以来表单的验证都是不可 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(5)-EF增删改查by糟糕的代码
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(5)-EF增删改查by糟糕的代码 上一讲我们创建了一系列的解决方案,我们通过一个例子来看看层与层之间的关系 ...
- asp.net mvc4
select省市联动选择城市 asp.net mvc4 2014-05-24 16:48 by P.C ++, 159 阅读, 2 评论, 收藏, 编辑 本文在 http://www.cnblogs. ...
随机推荐
- 局部覆盖element-ui的默认样式
最近项目中遇到的问题,只想在某个页面里面单独更改element-ui的样式,而不影响全局 有两种方法: 1.在需要更改的组件里新增一个style标签[重点:不要加scoped],然后直接获取class ...
- 微信小程序上传多张图片,及php后台处理
微信小程序上传多张图片,级小程序页面布局直接来代码index.wxml <view class='body' style='width:{{windowWidth}}px;height:{{wi ...
- [ CTSC 2007 / BZOJ 2151 ] Backup / 种树
\(\\\) \(Description\) 给出一个数轴上\(N\)个点的坐标\(A_i\),选择\(K\)个点对,使得这\(K\)个点对每个点对的距离之和尽可能小. \(N\in [0,10^5] ...
- Java&Xml教程(一)简介
XML是广泛用于数据传输和存储的技术.Java语言提供个各种各样的API来解析XML,例如DOM.SAX.StAX.JAXB.也还有一些其他的API用于解析XML,例如JDOM.本教程的目的是探索使用 ...
- Glide4.0 centerCrop属性和圆角 冲突
首先致谢:https://blog.csdn.net/flyinbed_/article/details/75506062 咱们不是代码的生产者,只是代码的搬运工. 最近有个工作中有个需求就是展示的图 ...
- jQuery与js的区别,并有基本语法详解,
通过过一下对比,我们能很清楚的发现jquery与js的区别,运用jquery能大量减少代码量,不过js里面关于时间的setinterval和settimeout只能用js <script src ...
- 删除过期备份报错RMAN-06207 RMAN-06208解决方案
RMAN备份日志中出现了警告 日志文件目录如下: [root@erpdbs rmanback]# ll total 88 -rw-r--r-- 1 oraprod dba 81011 Sep 7 22 ...
- JS高级——静态成员与实例成员
静态成员:构造函数的属性和方法 实例成员:实例化之后对象的属性和方法 // $("#id").css(); // $("#id").text(); // $.t ...
- highcharts 组合chart
/** *制作 复杂的组合型的 charts * *@param [options] 图表的默认配置 *@dependence jQuery.highcharts *@author wch */ fu ...
- 易买网之smartupload实现文件上传
经过俩个星期的奋斗,易买网项目完工.在之前,实现图片的上传,走过许多弯路,原来是好多基础的知识忘记了,没把smartupload文件包添加组件jar包至WEB-INF/lib包中,在此特别重视,做下文 ...