ASP.NET MVC2使用Ajax类库实例
.net for JavaScript
使用Jquery更新
<h1>Product Search -jQuery</h1>
<form action=”<%=Url.Action(“ProductSearch”) %>” method=”post” id=”jform”>
<%= Html.TextBox(“query”, null, new {size=40}) %>
<input type=”submit” id=”jsubmit” value=”go” />
</form>
<div id=”results2”>
<%Html.RenderPartial(“ProductSearchResults”, ViewData.Model); %>
</div>
<script src=”/Scripts/jquery-1.4.1.js” type=”text/javascript”></script>
<script src=”/Scripts/jquery-form.js” type=”text/javascript”></script>
<script type=”text/javascript”>
$(document).ready(function() {
$(‘#jform’).submit(function() {
$(‘#jform’).ajaxSubmit({ target: ‘#results2’ });
return false;
});
});
</script>
.net 实现
<form action="/Home/HelloAjax" method="post"
onsubmit=”Sys.Mvc.AsyncForm.handleSubmit(this, new Sys.UI.DomEvent(event),
{ insertionMode: Sys.Mvc.InsertionMode.replace,
updateTargetId:'result'});">
例子:
autoComplete Textbox
使用asp.net ajax类库实现
使用jquery实现
-使用asp.net ajax类库实现
服务器端:
[WebService(Namespace = “http://tempuri.org/”)]
[WebServiceBinding(ConformsTo= WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]// js 调用 ScriptService
public class ProductService: System.Web.Services.WebService
{[WebMethod]
public string[] ProductNameSearch(string prefixText, intcount){NorthwindDataContextdb= new NorthwindDataContext();
string[] products = (from p in db.Products
where p.ProductName.StartsWith(prefixText)
select p.ProductName).Take(count).ToArray();
return products;}}
前端调用:
<script src=”/Scripts/MicrosoftAjax.debug.js”type=”text/javascript”></script>
<script src=”/Scripts/ExtenderBase/BaseScripts.js”type=”text/javascript”></script>
<script src=”/Scripts/Common/Common.js”type=”text/javascript”></script>
<script src=”/Scripts/Compat/Timer/Timer.js”type=”text/javascript”></script>
<script src=”/Scripts/Animation/Animations.js”type=”text/javascript”></script>
<script src=”/Scripts/Animation/AnimationBehavior.js”type=”text/javascript”></script>
<script src=”/Scripts/PopupExtender/PopupBehavior.js”type=”text/javascript”></script>
<script type=”text/javascript”>
Sys.Application.add_init(function () {
$create(AjaxControlToolkit.AutoCompleteBehavior, {
serviceMethod: ‘ProductNameSearch’,
servicePath: ‘/ProductService.asmx’,
minimumPrefixLength: 1,completionSetCount: 10
},null,null,$get('query'))});</script>
-使用jquery实现
通过下拉框筛选数据
服务器端:
public ActionResultProductByCategory(intid){
NorthwindDataContext db= new NorthwindDataContext();
IList<Product> products = (from p in db.Products
where p.CategoryID== id
select p).ToList();
if (Request.IsAjaxRequest())
{
return View(“ProductSearchResults”, products);
}else{
return View(“ProductSearch”, products);
}
}
view层
var categories=db.Categories;
ViewData["CategoryID"]=new SelectList(Categories,"CategoryID","CategoryName");
<%=Html.DropDownList("CategoryID")%>
客户端:
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("#CategoryID").change(function(){
var selection =$("#CategoryID").val();
$("#results").load("/home/ProductByCategory/"+selection);// ajax请求
}}):
});
</script>
使用模态窗口
jquery 方式:
<script src=”/Scripts/jquery-1.4.1.js” type=”text/javascript”></script>
<script src=”/Scripts/jqModal.js” type=”text/javascript”></script>
<link href=”/Scripts/jqModal.css” rel=”stylesheet” type=”text/css” />
<script type=”text/javascript”>
$().ready(function() {
$(‘#dialog’).jqm();
});
</script>
<button class=”jqModal”>Click Me!</button>
<div class=”jqmWindow” id=”dialog”>
<a href=”#” class=”jqmClose”>Close</a>
<hr>
<h1>My Modal Box!</h2>
我是模态窗口</div>
评级控件
script src=”/Scripts/jquery-1.4.1.js” type=”text/javascript”></script>
<script src=”/Scripts/jquery.MetaData.js” type=”text/javascript”></script>
<script src=”/Scripts/jquery.rating.js” type=”text/javascript”></script>
<link href=”/Scripts/jquery.rating.css” rel=”stylesheet” type=”text/css” />
<input name=”rating” type=”radio” class=”star” value=”1”/>
<input name=”rating” type=”radio” class=”star” value=”2”/>
<input name=”rating” type=”radio” class=”star” value=”3”/>
<input name=”rating” type=”radio” class=”star” value=”4”/>
<input name=”rating” type=”radio” class=”star” value=”5”/>
服务器
public ActionResultRating(intrating) {
ViewData[“message”] = “Nothing selected”;
ViewData[“rating”] = 0;
if (rating.HasValue) {
ViewData[“rating”] = rating;
ViewData[“message”] = “You selected “+rating;
}
return View();
}
客户端
<%
intselectedRating= (ViewData[“Rating”] as int);
%>
<form action=”/home/rating” method=”post”>
<%for (int i = 1; i <= 5; i++) { %>
<input name=”rating” type=”radio” class=”star” value=”<%=i%>”
<%if(i<=selectedRating){ %> checked=”checked” <%}%>/>
<%} %>
<input type=”submit” value=”go” />
</form>
<br/>
<%= Html.Encode(ViewData[“Message”]) %>
页面:
<script src=”/Scripts/jquery-form.js” type=”text/javascript”></script>
<script language=”javascript” type=”text/javascript”>
$().ready(function() {
$(‘.star’).rating({
callback: function(value, link) {$(“#jform”).ajaxSubmit();});});
</script>
2011-4-23 0:27 danny
.net for JavaScript
使用Jquery更新
<h1>Product Search -jQuery</h1>
<form action=”<%=Url.Action(“ProductSearch”) %>” method=”post” id=”jform”>
<%= Html.TextBox(“query”, null, new {size=40}) %>
<input type=”submit” id=”jsubmit” value=”go” />
</form>
<div id=”results2”>
<%Html.RenderPartial(“ProductSearchResults”, ViewData.Model); %>
</div>
<script src=”/Scripts/jquery-1.4.1.js” type=”text/javascript”></script>
<script src=”/Scripts/jquery-form.js” type=”text/javascript”></script>
<script type=”text/javascript”>
$(document).ready(function() {
$(‘#jform’).submit(function() {
$(‘#jform’).ajaxSubmit({ target: ‘#results2’ });
return false;
});
});
</script>
.net 实现
<form action="/Home/HelloAjax" method="post"
onsubmit=”Sys.Mvc.AsyncForm.handleSubmit(this, new Sys.UI.DomEvent(event),
{ insertionMode: Sys.Mvc.InsertionMode.replace,
updateTargetId:'result'});">
例子:
autoComplete Textbox
使用asp.net ajax类库实现
使用jquery实现
-使用asp.net ajax类库实现
服务器端:
[WebService(Namespace = “http://tempuri.org/”)]
[WebServiceBinding(ConformsTo= WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]// js 调用 ScriptService
public class ProductService: System.Web.Services.WebService
{[WebMethod]
public string[] ProductNameSearch(string prefixText, intcount){NorthwindDataContextdb= new NorthwindDataContext();
string[] products = (from p in db.Products
where p.ProductName.StartsWith(prefixText)
select p.ProductName).Take(count).ToArray();
return products;}}
前端调用:
<script src=”/Scripts/MicrosoftAjax.debug.js”type=”text/javascript”></script>
<script src=”/Scripts/ExtenderBase/BaseScripts.js”type=”text/javascript”></script>
<script src=”/Scripts/Common/Common.js”type=”text/javascript”></script>
<script src=”/Scripts/Compat/Timer/Timer.js”type=”text/javascript”></script>
<script src=”/Scripts/Animation/Animations.js”type=”text/javascript”></script>
<script src=”/Scripts/Animation/AnimationBehavior.js”type=”text/javascript”></script>
<script src=”/Scripts/PopupExtender/PopupBehavior.js”type=”text/javascript”></script>
<script type=”text/javascript”>
Sys.Application.add_init(function () {
$create(AjaxControlToolkit.AutoCompleteBehavior, {
serviceMethod: ‘ProductNameSearch’,
servicePath: ‘/ProductService.asmx’,
minimumPrefixLength: 1,completionSetCount: 10
},null,null,$get('query'))});</script>
-使用jquery实现
通过下拉框筛选数据
服务器端:
public ActionResultProductByCategory(intid){
NorthwindDataContext db= new NorthwindDataContext();
IList<Product> products = (from p in db.Products
where p.CategoryID== id
select p).ToList();
if (Request.IsAjaxRequest())
{
return View(“ProductSearchResults”, products);
}else{
return View(“ProductSearch”, products);
}
}
view层
var categories=db.Categories;
ViewData["CategoryID"]=new SelectList(Categories,"CategoryID","CategoryName");
<%=Html.DropDownList("CategoryID")%>
客户端:
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("#CategoryID").change(function(){
var selection =$("#CategoryID").val();
$("#results").load("/home/ProductByCategory/"+selection);// ajax请求
}}):
});
</script>
使用模态窗口
jquery 方式:
<script src=”/Scripts/jquery-1.4.1.js” type=”text/javascript”></script>
<script src=”/Scripts/jqModal.js” type=”text/javascript”></script>
<link href=”/Scripts/jqModal.css” rel=”stylesheet” type=”text/css” />
<script type=”text/javascript”>
$().ready(function() {
$(‘#dialog’).jqm();
});
</script>
<button class=”jqModal”>Click Me!</button>
<div class=”jqmWindow” id=”dialog”>
<a href=”#” class=”jqmClose”>Close</a>
<hr>
<h1>My Modal Box!</h2>
我是模态窗口</div>
评级控件
script src=”/Scripts/jquery-1.4.1.js” type=”text/javascript”></script>
<script src=”/Scripts/jquery.MetaData.js” type=”text/javascript”></script>
<script src=”/Scripts/jquery.rating.js” type=”text/javascript”></script>
<link href=”/Scripts/jquery.rating.css” rel=”stylesheet” type=”text/css” />
<input name=”rating” type=”radio” class=”star” value=”1”/>
<input name=”rating” type=”radio” class=”star” value=”2”/>
<input name=”rating” type=”radio” class=”star” value=”3”/>
<input name=”rating” type=”radio” class=”star” value=”4”/>
<input name=”rating” type=”radio” class=”star” value=”5”/>
服务器
public ActionResultRating(intrating) {
ViewData[“message”] = “Nothing selected”;
ViewData[“rating”] = 0;
if (rating.HasValue) {
ViewData[“rating”] = rating;
ViewData[“message”] = “You selected “+rating;
}
return View();
}
客户端
<%
intselectedRating= (ViewData[“Rating”] as int);
%>
<form action=”/home/rating” method=”post”>
<%for (int i = 1; i <= 5; i++) { %>
<input name=”rating” type=”radio” class=”star” value=”<%=i%>”
<%if(i<=selectedRating){ %> checked=”checked” <%}%>/>
<%} %>
<input type=”submit” value=”go” />
</form>
<br/>
<%= Html.Encode(ViewData[“Message”]) %>
页面:
<script src=”/Scripts/jquery-form.js” type=”text/javascript”></script>
<script language=”javascript” type=”text/javascript”>
$().ready(function() {
$(‘.star’).rating({
callback: function(value, link) {$(“#jform”).ajaxSubmit();});});
</script>
2011-4-23 0:27 danny
发表评论
-
10.2Asp.net MVC各层使用TDD方式
2011-05-01 12:09 804Asp.net MVC各层使用TDD方式 Asp.net M ... -
10.1TDD简介
2011-04-30 23:09 558MVC中的测试驱动开发 为什么需要TDD TDD的由来 它 ... -
9、MVC程序安全限定
2011-04-29 22:14 1811常见网络安全攻击隐患 针对Asp.net MVC防御体系 安 ... -
8.1Ajax类库介绍
2011-04-28 09:12 885在ASP.NET MVC中使用 -Microsoft ASP. ... -
7.2自定义开发Filter
2011-04-27 16:04 625Custom Filters ➤IAuthorization ... -
7.1Filter的使用
2011-04-26 22:20 501Filter的使用 -对Action的附加说明 Asp.n ... -
6.3Action的调用与属性
2011-04-26 09:43 666Action的调用与属性 1、唤起Action R ... -
6.2ActionResult的使用
2011-04-25 19:14 683ActionResult public abstract c ... -
6.1Controller类的基本构成
2011-04-25 09:17 669关于Controller Controller是什么? C ... -
5.6在Webform中使用routing
2011-04-23 22:48 669在Webform中使用routing 在Asp.net 4中使 ... -
5.5URL和Action匹配的秘密
2011-04-22 09:11 901Routes是如何把URL映射为Action -请求路由管道 ... -
5.4使用routing生成URL
2011-04-21 11:05 601使用routing生成URL URL生成器概述 1、质询每个 ... -
5.3使用Routes
2011-04-20 09:11 7405.3使用Routes 注册Area Route publi ... -
5.2Routes匹配URL的工作方式
2011-04-19 08:55 852URL的匹配规则 -site/{controller}/{ac ... -
5.1Url和Routes介绍
2011-04-17 12:05 6495.1Url和Routes介绍 关于Urls -域名好记好拼 ... -
4、View引擎介绍
2011-04-16 22:03 649View引擎介绍 Request =>Routing= ... -
3、HtmlHelper类(续)
2011-04-16 13:41 652HtmlHelper类(续) Html.TextArea 用 ... -
2、HtmlHelper类
2011-04-15 22:33 1103HtmlHelper类 <%Html. MVC1 & ... -
1、View层
2011-04-15 09:06 629ViewDataDictionary类 View 负责输出 ...
相关推荐
1.1.6 ASP.NET 3.5 AJAX 1.2 .NET应用程序需框架 1.2.1 什么是.NET应用程序框架 1.2.2 公共语言运行时(CLR) 1.2.3 .NET Framework 类库 1.3 安装Visual Studio 2008 1.3.1 安装Visual Studio 2008 1.3.2 主窗口 ...
1.1.6 ASP.NET 3.5 AJAX 1.2 .NET 应用程序需框架 1.2.1 什么是.NET 应用程序框架 1.2.2 公共语言运行时(CLR) 1.2.3 .NET Framework 类库 1.3 安装Visual Studio 2008 1.3.1 安装Visual Studio 2008 1.3.2 主窗口 ...
这是整部学习资料 由于太大第一章免费供应给大家 在我的上传资源中 如果觉得还不过希望大家给个好评 当然具体本书的作者就不深究了把! 第1章 认识ASP.NET 3.5 ...17.4.5 ASP.NET MVC URL路由(URLRouting)...
1.1.6 ASP.NET 3.5 AJAX 1.2 .NET应用程序需框架 1.2.1 什么是.NET应用程序框架 1.2.2 公共语言运行时(CLR) 1.2.3 .NET Framework 类库 1.3 安装Visual Studio 2008 1.3.1 安装Visual Studio 2008 1.3.2 主窗口 ...
1.1.6 ASP.NET 3.5 AJAX 1.2 .NET应用程序需框架 1.2.1 什么是.NET应用程序框架 1.2.2 公共语言运行时(CLR) 1.2.3 .NET Framework 类库 1.3 安装Visual Studio 2008 1.3.1 安装Visual Studio 2008 1.3.2 主窗口 ...
此外,《ASP.NET 4高级程序设计(第4版)》专门提供了两章的内容来教你如何用Ajax 技术制作快速响应的页面,以及如何使用微软的ASP.NETAJAX平台。另外,还专门介绍了ASP.NET4 新增的功能,如MVC 和动态数据等。 ...
1.1.6 ASP.NET 3.5 AJAX 1.2 .NET应用程序需框架 1.2.1 什么是.NET应用程序框架 1.2.2 公共语言运行时(CLR) 1.2.3 .NET Framework 类库 1.3 安装Visual Studio 2008 1.3.1 安装Visual Studio 2008 1.3.2 主窗口 ...
此外,《ASP.NET 4高级程序设计(第4版)》专门提供了两章的内容来教你如何用Ajax 技术制作快速响应的页面,以及如何使用微软的ASP.NETAJAX平台。另外,还专门介绍了ASP.NET4 新增的功能,如MVC 和动态数据等。 ...