背景:

  博客中将构建一个小示例,用于演示在ASP.NET MVC4项目中,如何使用JQuery Ajax。

步骤:

1,添加控制器(HomeController)和动作方法(Index),并为Index动作方法添加视图(Index.cshtml),视图中HTML如下:

输入你的姓名:
<input type="text" id="txtName"/><br/>
输入你的年龄:
<input type="text" id="txtAge" /><br />
<button type="button" id="btn1">提交</button>
<button type="button" id="btn2">清空</button>
<p id="display"></p>

  视图中包含两个文本框,分别用来输入名字和年龄,包含连个按钮,分别用来提交信息和清空文本框的内容,同时包含一个段落,用来显示Ajax返回的数据信息。

2,在Home控制器中添加另外一个动作方(AddUsers),用来接收并处理视图传递过来的数据,并返回执行结果给视图,代码如下:

 1         public ActionResult AddUsers()
2 {
3 var my = new MyModel();
4 string result = string.Empty;
5 if(Request.IsAjaxRequest())
6 {
7 this.UpdateModel(my);
8 string name = my.Name;
9 int age = my.Age;
10 if (age < 18) result = name+"的文章好烂啊";
11 else result = name+",记得烂也要写";
12 }
13 return Content(result);
14 }

  如代码所示:直接用Content返回一个字符串。

  或者是返回一个 ContentResult()对象,与上面的代码类似(所以折叠了),代码如下:

 1         public ActionResult DoWithUsers()
2 {
3 var actionResult = default(ContentResult);
4 var my = new MyModel();
5 try
6 {
7 this.UpdateModel(my);
8 string name = my.Name;
9 int age = my.Age;
10 string temp = "";
11 if (age < 18) temp = "的文章好烂啊";
12 else temp = ",记得烂也要写";
13 actionResult = new ContentResult()
14 {
15 Content = name + temp
16 };
17 }
18 catch(Exception ex)
19 {
20 return null;
21 }
22 return actionResult;
23 }

3,修改Jquery&Ajax代码:

 1     $(document).ready(function () {
2 $("#btn1").click(function () {
3 var data = "";
4 var name = $("#txtName").val();
5 var age = $("#txtAge").val();
6 data += "&Name=" + encodeURI(name);
7 data += "&Age=" + encodeURI(age);
8 $.ajax({
9 async: true,
10 cache: false,
11 timeout: 60 * 60 * 1000,
12 data: data,
13 type: "GET",
14 datatype: "JSON",
15 url: "/Ajax/AddUsers",
16 success:function(result)
17 {
18 $("#display").text(result);
19 },
20 error: function (result) {
21 $("#display").html("error");
22 },
23 })
24 });

4,运行效果如图:

以上,最简单的ASP.NET MVC4&JQuery&AJax示例完成了。


以Json方式发送Action处理后的结果:

更多的情况下,不止是返回一个字符串,而是以Json的方式返回结果。

5,修改Action如下:

 1         public ActionResult DoWithUsers()
2 {
3 var my = new MyModel();
4 try
5 {
6 this.UpdateModel(my);
7 string name = my.Name;
8 int age = my.Age;
9 string temp = "";
10 if (age < 18) temp = "的文章好烂啊";
11 else temp = ",记得烂也要写";
12 JavaScriptSerializer jss = new JavaScriptSerializer();
13 return Json(jss.Serialize(new { Name = name, Message = temp }), JsonRequestBehavior.AllowGet);
14 }
15 catch(Exception ex)
16 {
17 return null;
18 }
19 }

说明:JSon方法返回一个JSonResult,而JSonResult同样是继承自ActionResult的。

6,修改AJax部分,代码如下:

1                 success:function(result)
2 {
3 result = JSON.parse(result);
4 $("#display").text(result.Name + result.Message);
5 },

运行效果一致。
以上,最简单的ASP.NET MVC4&JQuery&AJax&JSon示例完成。

摘自:https://www.cnblogs.com/SharpL/p/4641040.html

如何构建ASP.NET MVC4&JQuery&AJax&JSon示例的更多相关文章

  1. 练习 jquery+Ajax+Json 绑定数据 分类: asp.net 练习 jquery+Ajax+Json 绑定数据 分类: asp.net

    练习 jquery+Ajax+Json 绑定数据

  2. 如何构建 MVC&AJax&JSon示例

    背景: 博客中将构建一个小示例,用于演示在ASP.NET MVC4项目中,如何使用JQuery Ajax. 直接查看JSon部分 步骤: 1,添加控制器(HomeController)和动作方法(In ...

  3. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(44)-工作流设计-设计表单

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(44)-工作流设计-设计表单 系列目录 设计表单是比较复杂的一步,完成一个表单的设计其实很漫长,主要分为四 ...

  4. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(33)-数据验证共享

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(33)-数据验证共享 注:本节阅读需要有MVC 自定义验证的基础,否则比较吃力 一直以来表单的验证都是不可 ...

  5. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(7)-MVC与EasyUI DataGrid

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(7)-MVC与EasyUI DataGrid 没有源码的同学跳到第六讲下载源码再来. 我们需要漂亮的UI, ...

  6. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(1)-前言与目录(持续更新中...)

    转自:http://www.cnblogs.com/ymnets/p/3424309.html 曾几何时我想写一个系列的文章,但是由于工作很忙,一直没有时间更新博客.博客园园龄都1年了,却一直都是空空 ...

  7. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(48)-工作流设计-起草新申请

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(48)-工作流设计-起草新申请 系列目录 创建新表单之后,我们就可以起草申请了,申请按照严格的表单步骤和分 ...

  8. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(45)-工作流设计-设计步骤

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(45)-工作流设计-设计步骤 系列目录 步骤设计很重要,特别是规则的选择. 我这里分为几个规则 1.按自行 ...

  9. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(43)-工作流设计-字段分类设计

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(43)-工作流设计-字段分类设计 系列目录 建立好42节的表之后,每个字段英文表示都是有意义的说明.先建立 ...

随机推荐

  1. @Configurable

    spring的一个注解,用来自动注入bean的注解,不需要通过BeanFactory去获取    

  2. iOS Objective-C 中 bool 与 BOOL 的你不一定知道的事

    测试一下这段代码: - (void)test { NSLog(@"this is an attribut: %d", anAttribute); ; i < ; i++) { ...

  3. ssh 使用密钥无法登入Linux系统

    今天测试密钥登入linux系统时 出现如下问题: root@compute01:~# ssh alicxxx@xxx.com -p -i alickicxxxxxxx.key @@@@@@@@@@@@ ...

  4. 1416: Kick Ass Biu [几何]

    点击打开链接 1416: Kick Ass Biu [几何] 时间限制: 1 Sec 内存限制: 128 MB 提交: 174 解决: 35 统计 题目描述 在玩Kick Ass的时候,可以发现子弹的 ...

  5. 指针版P3690 【模板】Link Cut Tree (动态树)

    题面 传送门 题解 鉴于数组版实在是太慢我用指针版重新写了一遍 代码基本是借鉴了lxl某道关于\(LCT\)的题 //minamoto #include<bits/stdc++.h> #d ...

  6. 洛谷P5279 [ZJOI2019]麻将(乱搞+概率期望)

    题面 传送门 题解 看着题解里一堆巨巨熟练地用着专业用语本萌新表示啥都看不懂啊--顺便\(orz\)余奶奶 我们先考虑给你一堆牌,如何判断能否胡牌 我们按花色大小排序,设\(dp_{0/1,i,j,k ...

  7. linux互传文件nc命令

    使用nc命令可以很快的在两台主机传递文件,且不需要在同一网段,只要设置好端口即可. 一.安装(CentOS下) yum install -y nc  (需要root权限,可以用加上sudo) 二.使用 ...

  8. Ubuntu下增加eclipse菜单图标并配置java path(解决点击图标不能启动eclipse的问题)

    Ubuntu下增加eclipse菜单图标 Ubuntu的菜单图标在/usr/share/applications目录下. 1. 在/usr/share/applications目录下新建eclipse ...

  9. linux ln 命令,相当于windows快捷方式

    ln -s 源文件 目标文件. ln -s  **  **,它只会在你选定的位置上生成一个文件的镜像,不会占用磁盘空间, 硬链接ln ** **,没有参数-s, 它会在你选定的位置上生成一个和源文件大 ...

  10. dp--2019南昌网络赛B-Match Stick Game

    dp--2019南昌网络赛B-Match Stick Game Xiao Ming recently indulges in match stick game and he thinks he is ...