惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
InfoQ
Spread Privacy
Spread Privacy
GbyAI
GbyAI
F
Fortinet All Blogs
小众软件
小众软件
B
Blog RSS Feed
博客园_首页
量子位
Y
Y Combinator Blog
美团技术团队
H
Hackread – Cybersecurity News, Data Breaches, AI and More
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
大猫的无限游戏
大猫的无限游戏
Jina AI
Jina AI
T
The Blog of Author Tim Ferriss
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Vercel News
Vercel News
Last Week in AI
Last Week in AI
F
Full Disclosure
Stack Overflow Blog
Stack Overflow Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
宝玉的分享
宝玉的分享
Microsoft Azure Blog
Microsoft Azure Blog
有赞技术团队
有赞技术团队
A
About on SuperTechFans
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
The Cloudflare Blog
Hugging Face - Blog
Hugging Face - Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Tenable Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Project Zero
Project Zero
C
CXSECURITY Database RSS Feed - CXSecurity.com
Engineering at Meta
Engineering at Meta
博客园 - 叶小钗
S
SegmentFault 最新的问题
T
Threat Research - Cisco Blogs
博客园 - 司徒正美
MyScale Blog
MyScale Blog
云风的 BLOG
云风的 BLOG
V
V2EX
酷 壳 – CoolShell
酷 壳 – CoolShell
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
S
Schneier on Security
Latest news
Latest news
I
Intezer
A
Arctic Wolf
T
Threatpost

博客园 - 花拉子米

第一章、安装odoo的开发环境 [转] sp_getAppLock使用[转] sqlserver 索引优化 CPU占用过高 执行分析 服务器检查[转] python操作excel[转] 电子商城项目开发(后台功能模块开发) 【转】 电子商城项目开发(前台功能模块开发)【转】 sql server 全角与半角字符转换 sp_Msforeachtable与sp_Msforeachdb详解 sql server 查看索引碎片大小,并定期重建索引 Git安装 Git使用 sql 索引【转】 SVN安装使用【转】 c# 如何给 dataGridView里添加一个自增长列(列名为序号) MVC与ajax【转】 sql server block如何查询并kill 软件概要设计文档【转】 软件详细设计文档【转】 软件需求规格说明书【转】
asp.net mvc 使用Ajax调用Action 返回数据【转】
花拉子米 · 2018-12-08 · via 博客园 - 花拉子米

使用asp.net mvc 调用Action方法很简单。

一、无参数方法。

1、首先,引入jquery-1.5.1.min.js 脚本,根据版本不同大家自行选择。

<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>

2、在Controllers中书写前台Ajax需要调用的Action,比如:

public ActionResult test1()
        {
            return Content("返回一个字符串");
        }

这个Action返回了一个字符串。注意这里的返回,不是return View();

3、我们回到前台,假设我的功能是当页面中1个按钮单击时调用后台的Action并返回1个字符串。

<input type="text" id="txt1" name="txt1" />
<input type="button"  id="btnSub" name="btnSub" value="调用Action" />

如上,我的界面里放了1个文本框和1个按钮。那下面我们来实现当按钮单击时候调用后台方法返回字符并赋值给文本框的。

复制代码

<script type="text/javascript" language="javascript">
    $(document).ready(function () {
        $("#btnSub").click(function () {
            $.ajax({
                type: "POST",
                url: "/Home/test1",
                data: "",
                success: function (sesponseTest) {
                    $("#txt1").val(sesponseTest);
                }
            });
        });
    });
</script>

复制代码

很简单,下面大概介绍下用到的各个属性和方法。 $(document).ready(function () {};    -------类似我们原来写的<body onload="loat();">  页面加载方法。但是也有区别,具体请参考官方说明。

$("#btnSub").click(function () {};     -------按钮的单击事件。此处根据各自需要自行修改了。比如($("#btnSub").focus(function () {}……))等等。

$.ajax({});   -------ajax方法。

type:  -------  类型,此处为“POST”  还有  "GET"类型。

url:    -------  调用的Action   书写格式   /controller/action  

data:   -------  参数,因为此处没有,所以我们为""

success: function (sesponseTest) {}   -------  回调函数,就是当我的Action 执行完后,执行的方法。sesponseTest为Action返回的内容。

 $("#txt1").val(sesponseTest);   -------  把返回的字符串赋值给文本框。

 4、下面就是我们最终实现效果:当我们单击按钮的时候,通过Ajax调用了后台的1个Action 并返回一个字符串给文本框赋值了。

二、带参数方法。

我们实际项目中可能经常会遇到界面中需要传递1个或多个参数给Action,最终返回结果给界面的情况。那接下来我们就来看下带参数的调用方法。

1、在原来的Action基础上我们稍作改动。

 public ActionResult test1(string id)
        {
            return Content(id + "返回一个字符串");
        }

这个Action需要一个参数id  ,最后还是返回了一个字符画。

2、界面上我们再添加1个文本框。

<input type="text" id="txt1" name="txt1" /><br/>
<input type="text" id="txt2" name="txt2" /><br/>
<input type="button"  id="btnSub" name="btnSub" value="调用Action" />

此处有2个文本框,我将实现:点击按钮的时候把文本框1中的内容传递到Action进行处理,最终把返回结果显示在文本框2中。

复制代码

<script type="text/javascript" language="javascript">
    $(document).ready(function () {
        $("#btnSub").click(function () {
            var tvalue=$("#txt1").val();
            $.ajax({
                type: "POST",
                url: "/Home/test1",
                data: "id=" + tvalue,
                success: function (sesponseTest) {
                    $("#txt2").val(sesponseTest);
                }
            });
        });
    });
</script>

复制代码

细心的大家可能会发现,和上面无参数的就是多了点点改动。 这里的data: "id=" ……   带上了1个参数。id  就是我的Action 的参数的名称 id  。  然后把文本框1的值作为参数传递给Action.

多个参数呢,data的每个参数请用&&分开,如(data: "id=12345&&str=test",)……

注意这里的参数名称要和Action 的参数名称相同。

4、我们来看下最终效果。我们在文本框1中先输入内容,然后点击按钮,给文本框2赋值。