在ASP.MVC中使用Ajax

Asp.net MVC 抛弃了Asp.net WebForm那种高度封装的控件,让我们跟底层的HTML有了更多的亲近。可以更*、更灵活的去控制HTML的结构、样式和行为。Asp.net MVC可以更便捷的使用Ajax,本文针对开发过程中的技术进行了简单的总结并制作了一个小练习进行巩固。

1.准备工作

使用VS创建MVC应用程序,可以使用空模板,添加Home控制器以及对应的视图

使用NuGet进行包管理,添加Jquert、easyui等引用

2.开始制作Demo

UI:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAo0AAAD7CAIAAACAKznvAAAgAElEQVR4nO2dW2wUV7rv680P1hbiwU9ohKJoRCKDHAkeokSjo/AwW1vREMgEUBT65aCMMtJGymZCUHiA7EiR0nkAZQIZchhMGIfhjhsbm6vBCeALGBPjBl9wY2MbbIzdvjXQtpG+81Dd1etal263XXb/f/oE7qpVq9ZaXb3+a33rUkb/6KSN9Qy9aGx7VHXjXkNLd8ej6OPo8ycj8aHxyaHxyScj8cfR55HHww0t3VU37t1q6+0ZemEfGwwGg8FgME9mPBmbUlr/6GRTpK/6Vltrz+Dg+FT02UsbG4xNtT+KVt9qb4r09Y9O6uKEwWAwGAzmyYy+0UnZIv1jdXcf/nb/0ZPRiaHYlEt7Mhr/7f6jursPI/1jymhhMBgMBoN5MqNvZFKwzifj1Q1tbd1DA6MTQ+NTnmxgdKK9J3r5Zuvpy7fKf7kNg8FgMBgsEzMej0yy1tE3dvW3SMfjkcHxqbSts3/sRvOD0bHYSwAAAABkgNE3OmVZ9+CLurvdbT3RwdjLDK29N9p8v2dicooAAAAAkC5cf/p2R19j26On41PTYo1tjx48ejLbGQQAAADmMMajkUnTOvrHapo7uwZibqaf3WjuaLgbsQ/TNRC7ea9rLPZ8tvMIAAAAzFWMxyNTpjW0Pg4/GHg6/tLRrtT+tv/fp/956PT1Wy32IcMPBlo7+3T3fv78+d27dy9fvlxeXl5eXn758uW7d+8+fw5dBwAAABIYj0amHo1MPRyMX751//HIxMD4S3u7XPfbgWMVh8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/At0GgAAAPAv0GkAAADAv0CnAQAAAP8CnQYAAAD8C3QaAAAA8C/QaQAAAMC/QKcBAAAA/wKdBgAAAPwLdBoAAADwL9BpAAAAwL9ApwEAAAD/Ap0GAAAA/At0GgAAAPAv0GkAAADAv/x/8HYjzyI9q4AAAAAASUVORK5CYII=" alt="" />

html和JS代码如下:

 @{
ViewBag.Title = "Index";
} <html>
<head>
<title>
</title>
<script src="~/Script/jquery.min.js"></script>
<link href="~/Script/jquery-easyui-1.4.3/themes/default/easyui.css" rel="stylesheet" />
<script src="~/Script/jquery-easyui-1.4.3/jquery.easyui.min.js"></script>
<script src="~/Script/jquery-easyui-1.4.3/locale/easyui-lang-zh_CN.js"></script>
<script>
$(function () {
//使用$.ajax请求
$("#btn").click(function () {
var getText = $("#txt").val();
$.ajax({
url: 'GetGradeByName',
type: 'post',
data: { "name": getText },
success: function (data) {
if (data) {
alert(MSG(data))
}
else {
alert("没找到")
}
}
})
}) //使用$.post请求
$("#btn2").click(function () {
var getText = $("#txt").val();
$.post("/Home/GetGradeByName", { "name": getText }, function (data) {
if (data) {
alert(MSG(data))
}
else {
alert("没找到")
}
})
}) //使用$.get请求
$("#btn1").click(function () {
var getText = $("#txt").val();
$.get("/Home/GetGradeByName?name=" + getText, function (data) {
if (data) {
alert(MSG(data))
}
else {
alert("当前学生不存在")
}
})
}) $("#a1").click(function () { $.ajax({
url: 'JsonObject',
success: function (data) {
alert("json字符串是" + data.list);
var jsonObj = $.parseJSON(data.list);
alert("长度是" + jsonObj.length); $("#tb").datagrid({
columns: [[
{ field: 'stuName', width: },
{ field: 'stuGrad', width: } ]],
pagination: true
})
$('#tb').datagrid('loadData', { total: jsonObj.length, rows: jsonObj }); }
})
}) })
function MSG(s) {
return "恭喜!您的成绩是:" + s;
}
</script>
</head>
<body>
<h1 style=" color:red">ASP.MVC关于Ajax小练习</h1>
@Html.Label("学生姓名:")
@Html.TextBox("txt")
<input id="btn" type="button" value=" $.ajax查询" />
<input id="btn1" type="button" value="$.get查询" />
<input id="btn2" type="button" value="$.post查询" />
<br>
@Html.Label("提示:可以输入 小增、小舒、小郑、小陈", new { style = " color:#ccc" })
<br>
<a id="a1" href="javascript:void(0)">$.ajax返回json字符串</a>
<table id="tb" style="width:50%"></table>
</body>
</html>

HomeController代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization; namespace Ajax.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/ public ActionResult Index()
{
return View();
} /// <summary>
/// 说明:根据学生姓名获取成绩
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public string GetGradeByName(string name)
{
StudentGrade studentGrade = new StudentGrade();
IList<StudentGrade> getList = studentGrade.getAllGrades();
string s="";
foreach (StudentGrade model in getList)
{
if (model.stuName == name)
{
s = model.stuGrad;
}
}
string sss = s;
return s;
} /// <summary>
/// 返回学生列表的Jason字符串
/// </summary>
/// <returns></returns>
public ActionResult JsonObject()
{
StudentGrade studentGrade = new StudentGrade();
IList<StudentGrade> getList = studentGrade.getAllGrades();
JsonResult jsonResult = new JsonResult();
jsonResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
JavaScriptSerializer serializer = new JavaScriptSerializer();
string st =HttpUtility.HtmlDecode( serializer.Serialize(getList));
jsonResult.Data = new
{
list = st
};
return jsonResult;
}
} /// <summary>
/// 学生成绩帮助类
/// </summary>
public class StudentGrade
{
public StudentGrade() { } public IList<StudentGrade> getAllGrades()
{
IList<StudentGrade> list = new List<StudentGrade>()
{
new StudentGrade(){
stuGrad="",
stuName="小增"
},
new StudentGrade(){
stuGrad="",
stuName="小郑"
},
new StudentGrade(){
stuName="小舒",
stuGrad=""
},
new StudentGrade(){
stuGrad="",
stuName="小陈"
}
};
return list; }
public string stuName { get; set; } public string stuGrad { get; set; }
}
}

$.ajax的参数简单的说明如下:

http://blog.sina.com.cn/s/blog_4f925fc30100la36.html

3.演示

(1) 通过实体帮助类构造泛型 学生 对应 成绩列表进行显示(模拟数据库操作)

(2) 用户可以输入学生姓名进行成绩查询

(3) 输出查询结果,如果学生不存在则提示

在ASP.MVC中使用Ajax

上一篇:[Spring Boot] 使用多个Servlet


下一篇:cocos2d-x游戏开发 跑酷(八) 对象管理 碰撞检測