zoukankan      html  css  js  c++  java
  • 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>
    }


  • 相关阅读:
    用iptables封杀内网的bt软件
    FreeBSD 利用IPFW实现限制局域网使用QQ
    网络安全设备Bypass功能介绍及分析
    活用Windows Server 2008系统的几种安全功能
    恢复mysql管理员密码
    远程控制Windows2003下安装Pcanywhere导致Awgina.dll出错的解决办法
    Ubuntu 11.04 LAMP+JSP环境安装过程
    hbase首次导入大批次的数据成功!
    Chubby是什么?
    DP-Triangle
  • 原文地址:https://www.cnblogs.com/bignine/p/13778917.html
Copyright © 2011-2022 走看看