基本AJAX的例子在ASP.NET MVC? [英] Basic AJAX example with ASP.NET MVC?
问题描述
我在做一个演示ASP.NET MVC应用程序用于教育目的的过程。
I'm in the process of making a demo ASP.NET MVC app for educational purposes.
我有一个图像/链接标志后作为进攻。我想从服务器通过AJAX请求来标记进攻和检查,以确保用户有这个能力。
I have an image/link that flags a post as offensive. I would like to request from the server via AJAX to flag offensive and check to make sure that the user has this ability.
如果用户的话,那么我想标记后作为进攻中的数据库,并返回该标志经历。如果用户最终不必标志商品的权利话,我想返回一个负面的信息给客户端,所以我可以弹出一个漂亮的jQuery的框,说明它没有通过。
If the user does, then I want to flag the post as offensive in the database and return that the flag went through. If the user ends up NOT having the right to flag items then I would like to return a negative message to the client so I can popup a nice jQuery box stating that it didn't go through.
我想要做的这一切,没有一个完整的回发/刷新。
I'm trying to do this all without a full postback/refresh.
没有人有简单的AJAX请求正在与MVC例子任何联系?
Does anyone have any links to examples of simple AJAX requests being made with MVC?
推荐答案
这其实是pretty的易于使用jQuery。比方说,你的链接是这样的:
It is actually pretty easy with jQuery. Let's say your link is something like this:
<a href="javascript:flagInappropriate(<%=Model.PostId%>);">Flag as inappropriate</a>
创建一个JavaScript,调在你的控制器动作,检查和标记:
Create a javascript to call the action in your controller to check and flag as necessary:
function flagInappropriate(postId) {
var url = "<CONTROLLER>/<ACTION>/" + postId;
$.post(url, function(data) {
if (data) {
// callback to show image/flag
} else {
// callback to show error/permission
}
});
}
在你的控制器,你的操作方法很可能是这样的:
In you action method in your controller will probably look like this:
[AcceptVerbs("POST")]
public bool FlagAsInappropriate(int id) {
// check permission
bool allow = CheckPermission();
// if allow then flag post
if (allow) {
// flag post
return true;
} else {
return false;
}
}
这篇关于基本AJAX的例子在ASP.NET MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!