使用JQuery Ajax和ASP的正确模式是什么?净Mvc吗?

时间:2021-09-09 06:07:01

I'm very new to both the Mvc framework as well as JavaScript and JQuery. I'm trying to understand the right way to structure Ajax calls.

我对Mvc框架以及JavaScript和JQuery都很陌生。我正在尝试理解构造Ajax调用的正确方法。

Let's say I have a "Vote Up" button similar to what you see on *. When the user clicks it I need to update the vote count in the database and return the new value to the UI. Currently I am achieving this by having an action called "VoteUp" on the "PostsController" which takes an "int postID" as a parameter.

假设我有一个“向上投票”按钮,类似于你在*上看到的。当用户单击它时,我需要更新数据库中的选票计数,并将新值返回给UI。目前,我通过在“PostsController”上有一个名为“VoteUp”的操作来实现这一点,该操作以“int postID”作为参数。

public PostsController : Controller
{
    public ActionResult VoteUp(int postId)
    {
        //Increment Post Vote Count
        return Json(voteCount); //Return just the new vote count as a JSon result.
    }
}

I'm then calling this method via ajax by invoking the url "http://mydomain.com/posts/voteUp?postId=5". I then return an JSon ActionResult with the new value to update the UI with.

然后通过ajax调用url“http://mydomain.com/posts/voteUp?postId=5”调用这个方法。然后返回一个带有新值的JSon ActionResult以更新UI。

Is this the right way to implement this? Again, I'm totally new to both javascript and jquery. I'm used to doing everything as click event handlers in asp.net webforms. Any guidance would be appreciated.

这是正确的实现方法吗?同样,我对javascript和jquery都完全陌生。我习惯了在asp.net webforms中作为单击事件处理程序执行所有的操作。如有任何指导,我们将不胜感激。

2 个解决方案

#1


12  

Yes, it sounds like you've got it about right.

是的,听起来你说得对。

Note, however, that if you change postId to Id, then you could call with a URL like:

但是,请注意,如果将postId更改为Id,则可以使用以下URL进行调用:

http://example.com/posts/voteUp/5

http://example.com/posts/voteUp/5

(With the default routing.) It's a question of personal preference.

(使用默认路由)。这是个人喜好问题。

#2


3  

I would approach this using jQuery and JsonResult Controller. Your jQuery code would call the JsonResult which would pass the pertinent information off to the model code to handle adding a new vote. I wrote a brief tutorial on similar concepts which is available at http://www.dev102.com/2008/08/19/jquery-and-the-aspnet-mvc-framework/

我将使用jQuery和JsonResult控制器来实现这一点。您的jQuery代码将调用JsonResult,它将把相关信息传递给模型代码,以处理添加新选票的问题。我在http://www.dev102.com/2008/08/19/jquery-and- aspnet-mvc-framework/上写了一篇关于类似概念的简短教程

#1


12  

Yes, it sounds like you've got it about right.

是的,听起来你说得对。

Note, however, that if you change postId to Id, then you could call with a URL like:

但是,请注意,如果将postId更改为Id,则可以使用以下URL进行调用:

http://example.com/posts/voteUp/5

http://example.com/posts/voteUp/5

(With the default routing.) It's a question of personal preference.

(使用默认路由)。这是个人喜好问题。

#2


3  

I would approach this using jQuery and JsonResult Controller. Your jQuery code would call the JsonResult which would pass the pertinent information off to the model code to handle adding a new vote. I wrote a brief tutorial on similar concepts which is available at http://www.dev102.com/2008/08/19/jquery-and-the-aspnet-mvc-framework/

我将使用jQuery和JsonResult控制器来实现这一点。您的jQuery代码将调用JsonResult,它将把相关信息传递给模型代码,以处理添加新选票的问题。我在http://www.dev102.com/2008/08/19/jquery-and- aspnet-mvc-framework/上写了一篇关于类似概念的简短教程