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("幸福摩天轮")
posted @ 2020-10-12 20:02  周兴兴  阅读(186)  评论(0编辑  收藏  举报