• Razor视图引擎


    ASP中<% %>=@

    语法规则

    Razor 代码封装于 @{ ... } 中

    行内表达式(变量和函数)以 @ 开头

    代码语句以分号结尾

    字符串由引号包围

    C# 代码对大小写敏感

    C# 文件的扩展名是 .cshtml

    @{ var name = "Word!!!!";}
              @for (int i = 0; i < 3; i++)
              {
                    <h1>Hello @name</h1>
              }
            @if (true)
            {
                  <h1>Hello @name</h1>
            }

    Public ActionResult Loop()
    {
    //ViewBag是动态类型 ViewBag.Loop=new List<string>{"张三","李四","王五"}; return View(); }


    例子
    public ActionResult Index()
            {
                TestObjectService();

                using (RazorDemo.Models.MySchoolEntities entities = new Models.MySchoolEntities())
                {
                    return View(entities.Students.ToList());
                }
                return View();
               
            }

            public ActionResult Test()
            {
                return View();
            }

            public void TestEntityClient()
            {
                using (EntityConnection conn = new EntityConnection("name=MySchoolEntities"))
                {
                    conn.Open();
                    EntityCommand cmd = conn.CreateCommand();
                    cmd.CommandText = @"SELECT VALUE c FROM  MySchoolEntities.Students AS c WHERE c.StudentNo = 23214";
                    EntityDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
                    while (rdr.Read())
                        Console.WriteLine(rdr[2].ToString());
                    rdr.Close();
                }
               
            }


            public void TestObjectService()
            {
                using (ObjectContext context = new ObjectContext(("name=MySchoolEntities")))
                {
                    string eSql = "SELECT VALUE c FROM  MySchoolEntities.Students AS c";
                    ObjectQuery<Student> query = context.CreateQuery<Student>(eSql);
                    foreach (Student stu in query.ToList())
                    {
                        Console.WriteLine(stu.StudentName);//输出学生姓名
                    }
                }
            }
    前台:
    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.StudentNo)
            </td>

            <td>
                @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
            </td>
        </tr>
    }


  • 相关阅读:
    如何根据二叉树 前序遍历 中序遍历 后序遍历 中的两种遍历来反推另一种遍历
    dijkstral改编
    纪念做出来的第一道计算几何题
    链式前向星
    一道简单树形dp
    算法进阶指南—特殊排序
    算法进阶指南二分章节的两道题
    秦皇岛winter camp 总结
    C
    一道cf水题
  • 原文地址:https://www.cnblogs.com/bignine/p/13778917.html
Copyright © 2020-2023  润新知