• 分部视图 Partial View


    Partial View:可以应用于其他View中以作为其中一部分的View的片段。像类(class)一样,编写一次, 然后在其他View中被反复使用。(就是为了避免冗余,写一个通用的view,当用到时直接调用,不用再敲一遍)

    示例:

    1、在Controllers中新添加一个控制器->MVCDemoController在views中生成一个文件夹MVCDemo,右键添加->MVC 5 分布页(Partial View):_PartialPageDateTime.cshtml

    其中添加代码@ViewBag.DateTime

    2、MVCDemoController.cs中新建两个Action,SharedDateDemo和PartialViewDate。

     public ActionResult SharedDateDemo()
            {
                ViewBag.DateTime = DateTime.Now;
                return View();
            }
            [ChildActionOnly]
            public ActionResult PartialViewDate()
            {
                ViewBag.DateTime = DateTime.Now.AddMinutes(10);
                return PartialView("_PartialPageDateTime");
            }

    注意[ChildActionOnly] 表示这个Action只应作为子操作进行调用。也就是说直接通过 controller/action这样的网址是不能访问的,会提示只能由子请求访问的错误。

    3、在视图SharedDateDemo中添加相应的代码

    <body>
        <div>
            <h2>SharedDateDemo</h2>
            <h1>主体View中的时间值</h1>
            @ViewBag.DateTime
            <h1>使用@@Html.Partial中的时间值</h1>
            @Html.Partial("_PartialPageDateTime")
            <h1>使用@@Html.View中的时间值</h1>
            @Html.Action("PartialViewDate")
        </div>
    </body>

      <h1>使用@@Html.View中的时间值</h1>
            @Html.Action("PartialViewDate")

    调用了action(PartialViewDate)

    很像以前学的类(class)的调换、、、、

  • 相关阅读:
    CentOS 6.4 利用 Awstats 7.2 分析 Nginx 日志
    CentOS 6.4 x64 postfix + dovecot + 虚拟用户认证
    配置日志logwarch 每天发送到邮箱
    CentOSx64 安装 Gearmand 和 Gearman php扩展
    Redis之基本介绍
    Synchronized锁的基本介绍
    Lock的基本介绍
    java之threadlocal的使用
    Java之事务的基本应用
    linux之cp和scp的使用
  • 原文地址:https://www.cnblogs.com/wwr01/p/7725873.html
Copyright © 2020-2023  润新知