“21天好习惯”第一期-8

Ajax实现局部刷新效果

视频链接AJax局部刷新

 Ajax简介

  1. Ajax是Asynchronous JavaScript and XML(异步JavaScript与XML)的缩写。这是在后台请求服务器数据,而不必重载Web页面的一种技术。
  2. ASP.NET MVC 5是一个现代Web框架,并且与其他现代Web框架一样,它从一开始就支持Ajax技术。Ajax支持的核心来自于jQuery。
  3. ASP.NET MVC 5框架中包含了对Ajax的支持。

 

 

实现局部刷新

HomeController.cs

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AjaxDemo.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetDate() {
            return Content(DateTime.Now.ToString());  
        }
      
    }
}

index.cshtml

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script src="~/Scripts/jquery-3.4.1.js"></script>
        <script type="text/javascript">
            $(function () {
                $("#btn").click(function () {
                    $.ajax({
                    //访问的action
                    url: "/home/getdate",
                        type: "post",
                    //success,但ajax请求成功时,会返回数据,方法中data就是返回的数据
                        success: function (data) {
                            //alert(data);
                            $("#show").html(data);
                    },
                    data: "id=2 & name=23"
                     });
                });
            });

        </script>

    <title></title>
    </head>
    <body>
        @* 点击按钮从服务器获取时间,显示在id为show标签上*@
        <input type="button" id="btn" value="获取时间" />
        获取到的时间;
        <div id="show" style="width:300px;height:30px; border:1px dotted silver"></div>


    </body>
</html>

使用Ajax.ActionLink

  1. Ajax辅助方法
  2. 同样可以用于创建表单和指向控制器操作方法的链接,但不同的是,它们与服务器采用的是Ajax(异步交互)方式
  3. 当使用Ajax辅助方法时,无须编写任何脚本代码即可实现程序的异步性。■脚本
  4. 使用Ajax辅助方法必须先引入jQuery.unobtrusive-ajax.js■此文件默认包含在ASP.NET MVC应用程序模板中。 

insterAfter:刷新在数据后面进行插入

insterBrfore:刷新在数据前面插入

Replace:刷新替换之前的数据

index.cshtml

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="~/Scripts/jquery-3.4.1.js"></script>
    <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
    <script type="text/javascript">
        function msg(data) {
            alert(data);
        }

    </script>
    <title></title>
</head>
    <body>
        @* 点击按钮从服务器获取时间,显示在id为show标签上*@
        <input type="button" id="btn" value="获取时间" /><br />
        @Ajax.ActionLink("获取时间新方法","getdata","home",new AjaxOptions { 
            Confirm="确认要提交吗?",
            HttpMethod="post",
            InsertionMode=InsertionMode.Replace,
           // OnSuccess="msg",
            UpdateTargetId="show"
        })
    <br />
        获取到的时间;
        <div id="show" style="width:300px;height:30px; border:1px dotted silver"></div>


    </body>
</html>

HomeComtroller.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AjaxDemo.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetDate() {
            return Content(DateTime.Now.ToString());  
        }
        public ActionResult UserRegister(string UserName,string Password)
        {
            System.Threading.Thread.Sleep(3000);
            ViewBag.name = UserName;
            ViewBag.pwd = Password;
            //将result视图的内容作为返回值,返回给id为show的一个div标签
            //PartialView分部视图
            // view
            return PartialView("result");
        }


    }
}

index.cshtml

 

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="~/Scripts/jquery-3.4.1.js"></script>
    <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
    <script type="text/javascript">
        function msg(data) {
            alert(data);
        }

    </script>
    <title></title>
</head>
<body>
    @* 点击按钮从服务器获取时间,显示在id为show标签上*@
    @using (Ajax.BeginForm("UserRegister", "home", null, new AjaxOptions()
        {
            Confirm = "确定注册嘛?" ,
            HttpMethod="post",
            InsertionMode = InsertionMode.Replace,
            LoadingElementId = "loading",
            UpdateTargetId = "show"

    } ))
    {
    <table class="table table-bordered table-hover">
        <tr>
            <td>用户名:</td>
            <td><input type="text" name="UserName"  /></td>
        </tr>
        <tr>
            <td>密码: </td>
            <td><input type="password" name="Password"  /></td>
        </tr>
        <tr >
            <td colspan="2"><input type="submit" value="注册" /></td>
        </tr>
    </table>

    }
    <br />
    获取到的时间;
    @*loading标签用于展示加载效果:show标签用于展示ajax请求返回的数据内容*@
    <div id="loading" style="display:none">数据加载中......</div>
    <div id="show"></div>


</body>
</html>

Result.cshtml

@{
    ViewBag.Title = "Result";
    string name = ViewBag.name;
    string password = ViewBag.pwd;
}
<div>
    <h2>结果</h2>
    <p>你注册的名称是:@name</p>
    <p>你注册的密码是:@password</p>
</div>

实例

包含查询字段

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Redmonster0923

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值