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)

  

原文地址:https://www.cnblogs.com/zhchoutai/p/6927674.html