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>
}


posted @ 2020-10-07 20:29  小九家的丫头  阅读(126)  评论(0编辑  收藏  举报