1. 程式人生 > 實用技巧 >C# 內聯Html Helper

C# 內聯Html Helper

Controller 程式碼

using System.Web.Mvc;

namespace Demo.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.
"; return View(); } public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); } } }

cshtml程式碼

@using Demo.Models
@{
    ViewBag.Title = "內聯Html Helpers";
}
<h2>@ViewBag.Title.</h2>
<
h3>@ViewBag.Message</h3> @*這是定義*@ @helper ShowMessage(string[] strArr) { <ol> @foreach (var item in strArr) { <li>@item</li> } </ol> } @helper HelloWorld(StudentModel studentModel) { <p>我是 @studentModel.Name,我來自 @studentModel.Address ,我想對世界說:@studentModel.Words</
p> } <p>程式語言集合:【 內聯Html Helpers 】</p> @*這裡是使用*@ @ShowMessage(new string[] { "Java", "C#", "Python"}) @HelloWorld(new StudentModel() { Name = "位元-周興興", Address = "我來自貴州省貴陽市花溪區貴州大學", Words = "我愛寫程式碼!" }) @CommonLnlineHelper.WriteCode("幸福摩天輪")