• (13)使用Ajax Helper 提高用户体验


    问题

    当你点击链接时,整个的网页都被重新加载。尤其是你仅仅一小点内容需要被更新时,这将被感觉是一个很慢的过程。

    解决方案

    更新之前创建的HTML.ActionLink 去调用ajax 帮助类。Ajax.ActionLink 仅仅去重新加载那些发生变化的内容。

    讨论

    MVC提供了几个给力的帮助类。到目前为止,这本书中已经广泛的应用了HTML Helper。在过去创建的所有view中,HTML helper至少都使用过一次。在这个秘方中,我们将使用AjaxHelper类替换掉Book/Index中的HtmlHelper 类。

    实现Ajax需要一点额外的设置才可以使用。通常情况下我发现这个额外的工作,可以打消开发人员使用它的念头。我要让大家知道,额外的安装设置是值得的,因为带来的好处是获得了更好的用户体验,这是非常值得努力去做的。

    步骤从web.config开始。2个关键的地方要被设置成true. ClientValidationEnabled 和UnobtrusiveJavaScriptEnabled。

    译者:原书中代码引入了整个web.config文件。我们只需要到appSettings节点下即可找到这两个keys。

    双击代码全选
    1
    2
    3
    4
    5
    <appSettings>
      <add key="webpages:Version" value="1.0.0.0" />
      <add key="ClientValidationEnabled" value="true" />
      <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    </appSettings>
     

    接下来的步骤是我们要引入几个javascript 文件。我们要在里shared 的layout文件夹里完成这件事,因为几乎我们创建所有的view时都会引用它(布局模板)。在Views/Shared /_Layout.cshtml 文件的<head>标签中。我们引入2个javascript 文件,代码如下:

    双击代码全选
    1
    2
    3
    4
    5
    6
    <head>
        <title>@ViewBag.Title</title>
        <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
        <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")"type="text/javascript"></script>
    </head>
     

    这些文件被自动的包含在基础MVC3应用程序中。(译者:你可以从scripts文件夹中找到他们)。以上的步骤完成AJAX的核心配置。接下来,我们要更新Book/index view。在下边的例子里,三个filter link和sortable header link将用Ajax.ActionLink 替换Html.ActionLink.代码如下:

    双击代码全选
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    @model PagedList.IPagedList<MvcApplication.Models.Book>
    @if (IsAjax)
    {
        Layout = null;
    }
    <h2>
        Title</h2>
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <p>
        Show:
        @if (ViewBag.CurrentFilter != "")
        {
            @Ajax.ActionLink("All", "Index",
                                new
                                {
                                    sortOrder = ViewBag.CurrentSortOrder,
                                    Keyword = ViewBag.CurrentKeyword
                                },
                                new AjaxOptions { UpdateTargetId = "main" })
        }
        else
        {
            @:All
            }
        &nbsp; | &nbsp;
        @if (ViewBag.CurrentFilter != "NewReleases")
        {
            @Ajax.ActionLink("New Releases", "Index", new
                            {
                                filter = "NewReleases",
                                sortOrder = ViewBag.CurrentSortOrder,
                                Keyword = ViewBag.CurrentKeyword
                            },
                            new AjaxOptions { UpdateTargetId = "main" })
        }
        else
        {
            @:New Releases
            }
        &nbsp; | &nbsp;
        @if (ViewBag.CurrentFilter != "ComingSoon")
        {
            @Ajax.ActionLink("Coming Soon", "Index", new
                            {
                                filter = "ComingSoon",
                                sortOrder = ViewBag.CurrentSortOrder,
                                Keyword = ViewBag.CurrentKeyword
                            },
                            new AjaxOptions { UpdateTargetId = "main" })
        }
        else
        {
            @:Coming Soon
            }
    </p>
    @using (Html.BeginForm())
    {
        @:Search: @Html.TextBox("Keyword")<input type="submit" value="Search" />
     
    }
    @Html.Partial("_Paging")
    <table>
        <tr>
            <th>
                @Ajax.ActionLink("Title", "Index", new
                                {
                                    sortOrder = ViewBag.TitleSortParam,
                                    filter = ViewBag.CurrentFilter,
                                    Keyword = ViewBag.CurrentKeyword
                                },
                                new AjaxOptions { UpdateTargetId = "main" })
            </th>
            <th>
                @Ajax.ActionLink("Isbn", "Index", new
                                {
                                    sortOrder = ViewBag.IsbnSortParam,
                                    filter = ViewBag.CurrentFilter,
                                    Keyword = ViewBag.CurrentKeyword
                                },
                                new AjaxOptions { UpdateTargetId = "main" })
            </th>
            <th>
                Summary
            </th>
            <th>
                @Ajax.ActionLink("Author", "Index", new
                                {
                                    sortOrder = ViewBag.AuthorSortParam,
                                    filter = ViewBag.CurrentFilter,
                                    Keyword = ViewBag.CurrentKeyword
                                },
                                new AjaxOptions { UpdateTargetId = "main" })
            </th>
            <th>
                Thumbnail
            </th>
            <th>
                @Ajax.ActionLink("Price", "Index", new
                                {
                                    sortOrder = ViewBag.PriceSortParam,
                                    filter = ViewBag.CurrentFilter,
                                    Keyword = ViewBag.CurrentKeyword
                                },
                                new AjaxOptions { UpdateTargetId = "main" })
            </th>
            <th>
                @Ajax.ActionLink("Published", "Index", new
                                {
                                    sortOrder = ViewBag.PublishedSortParam,
                                    filter = ViewBag.CurrentFilter,
                                    Keyword = ViewBag.CurrentKeyword
                                },
                                new AjaxOptions { UpdateTargetId = "main" })
            </th>
            <th>
            </th>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Title)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Isbn)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Summary)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Author)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Thumbnail)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Price)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Published)
                </td>
                <td>
                    @Html.ActionLink("Edit","Edit", new { id = item.ID }) |
                    @Html.ActionLink("Details","Details", new { id = item.ID }) |
                    @Html.ActionLink("Delete","Delete", new { id = item.ID })
                </td>
            </tr>
        }
    </table>
    @Html.Partial("_Paging")
     

    译者:上边代码标绿的地方就是我们更新的地方。

    年轻不是你玩的理由,而是你奋斗的资本
  • 相关阅读:
    7.JavaScript-Promise的并行和串行
    6.Javascript如何处理循环的异步操作
    5.Javascript闭包得实现原理和作用
    4.Javascript中实现继承的几种方法及其优缺点
    3.Javascript实现instanceof
    BEF
    ant-vue Table组件selectedRows翻页后不保留上一页已选
    js 构造函数、继承
    Vue全局注册组件
    react迷惑的点(一)
  • 原文地址:https://www.cnblogs.com/lyaxx1314/p/3603504.html
Copyright © 2020-2023  润新知