mvc 异步请求、异步连接、异步表单

》》》 利用Jquery ajax

》》》 mvc 异步表单

c# MVC 添加异步 jquery.unobtrusive-ajax.min.js 方法

具-->Nuget程序包管理器-->程序包管理器控制台

在控制台输入:PM>Install-Package Microsoft.jQuery.Unobtrusive.Ajax --version 3.0.0

回车执行即可在Scripts中看到改js文件

2、html代码 及视图代码

html 复制代码
<style type="text/css">
        #imgLoad {
            display: none;
        }
    </style>

@using (Ajax.BeginForm("Login_Ajax", "Login", new AjaxOptions {
 Confirm = "你是否确定要提交", 
 HttpMethod = "post", 
 InsertionMode = InsertionMode.InsertAfter,
 UpdateTargetId = "视图中html元素的ID", //  返回的信息,填充在这个ID中。
 OnSuccess = "back", //成功之后 方法的 js函数
 LoadingElemId="imgLoad" //视图中html元素的ID  一边服务器处理耗时,显示这个图片,请求之前是不显示的,请求显示,成功返回,就不显示了"
  }))
            {
               <table border="0" cellpadding="0" cellspacing="2">
                <tr>
                    <td width="330" height="331"> </td>
                    <td width="145"> </td>
                    <td width="62"> </td>
                    <td width="119"> </td>
                </tr>
                   
                <tr>
                    <td height="29"> </td>
                    <td>@Html.TextBoxFor(s => s.UserName, new { @tabindex = "1", @class = "login_text" })</td>
                    <td rowspan="2">
                        <button id="btnLogin" tabindex="3" class="button_1" type="submit" />
                    </td>
                    <td rowspan="2">@Html.ActionLink("注册账号", "Register") </td>
                </tr>
                <tr>
                    <td height="26"> </td>
                    <td>@Html.PasswordFor(s => s.PassWord, new { @tabindex = "2", @class = "login_text" })</td>
                    
                </tr>

                
            </table>
                
            }

 <div id="imgLoad">加载中~~~(一边是gif照片)</div>



VIEW 代码

html 复制代码
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="~/Scripts/jquery.min.js"></script>
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
    <style type="text/css">
        #imgload {
            display:none;
        }
    </style>
    <script type="text/javascript">

        function suc(data)
        {
            //  成功回调函数
        }
        function fal(data)
        {
             //  失败回调函数
        }
    </script>
</head>
<body>
    <div>        
      
        <hr>
        @Ajax.ActionLink("获取时间", "GetDateTime", new AjaxOptions
        {
          Confirm="你确定获取数据",
          HttpMethod="Post",
          LoadingElementId="imgload",
          InsertionMode = InsertionMode.InsertBefore,
          UpdateTargetId = "rst",
          OnSuccess="suc",
          OnFailure="fal"
        })
        <hr />
        <p>
            <label id="rst"></label>
        </p>
        <h1>异步表单:</h1>
        @using (Ajax.BeginForm("GetDateTime", "Home", new AjaxOptions()
        {
            //提交请求的方法
            HttpMethod = "post",
            //成功时执行的异步函数
            OnSuccess = "suc",
            OnFailure = "fal",
             InsertionMode = InsertionMode.InsertBefore,
            UpdateTargetId = "rst",
            //请求时加载的图片
            LoadingElementId = "imgload"
        }))
        {
            <input type="text" name="txtName" />
            <input type="submit" />
            
        }
       
        <img id="imgload" src="~/imgs/load.gif" />
       
    
    </div>
</body>
</html>

Controller

相关推荐
步行cgn14 小时前
HTTP 请求协议详解
spring·mvc
MoRanzhi120316 小时前
第3篇 · 使用 PyQt5 实现 2048 桌面版游戏:MVC 架构与撤销和重做
python·mvc·pyqt5·桌面应用开发·gui开发·2048游戏·撤销与重做
CoderYanger1 天前
Java EE 进阶:4.Spring Web MVC 入门(第一弹)
java·开发语言·前端·spring·面试·java-ee·mvc
小羊没烦恼!3 天前
Ajax与JSON的一些总结
android·java·前端·数据库·mysql·算法·okhttp
CarIise3 天前
Spring MVC 请求参数绑定与常用注解2
java·spring·mvc
CarIise3 天前
Spring MVC 请求参数绑定与常用注解
java·spring·mvc
脉动数据行情4 天前
Java OkHttp 对接国际期货批量行情 SpringBoot 定时落库实践
java·spring boot·okhttp
牧瀬クリスだ9 天前
Ajax入门:从原生XHR到axios实战
java·spring boot·ajax·okhttp
萧瑟余晖9 天前
Spring MVC文件上传SSE与WebSocket详解
websocket·spring·mvc
乌暮10 天前
# Spring 入门与 Spring MVC 实战指南:从零理解 IoC 到 RESTful 图书管理
java·开发语言·后端·学习·spring·mvc·restful