开发者

ASP.NET MVC: Entering in the controller again from the page via ajax?

can anyone help?

I have just started asp.net mvc and its all working.. entering my controller and then sending out the view... I 开发者_StackOverflow社区am also writing a log here....

The page that loads as quite a bit jquery and javascript so i need to be able to enter into the controller again preferably not a postback but via ajax or similar - is this possible?

Or do i call an action on a controller from javascript i suppose my question should be - ???

Any ideas?

Thanks in advance


Well if you want to do ajax against a controller you can do an .ajax with jQuery like this:

$.ajax({
                 type: "GET",//type of verb to use POST/GET
                 data: ({ Data:data }),//Send some data to the controller
                 url: '<% = Url.Action("MEthod","Controller")%>',//URL of the resource
                 dataType: "text/html", //Define the type of spectated results(JSON,Html,Xml)
                 timeout: 8000, //How much time to wait for the asynchronous call to complete
                 error: function() {
                     alert('ERROR');
                 }, //end error
                 success: function(html) {
                 //Here do something with the result hold in the html variable  
                 } //end succes
             });   //end ajax

For a complete reference you can visit the jQuery Documentation http://docs.jquery.com/Ajax


Your on the right track.

MVC's routing capabilities make it really easy to "enter a controller" again. Just make a POST or GET request using the jQuery ajax methods.

I'd post some code but I'm not sure what your trying to do.


For starters you have the Ajax helpers as part of the MVC Framework which have all sorts of helpers for MVC ajax tasks, check them out. Otherwise controllers and actions are normal requestable urls from whatever your ajax tool of choice is.


Something like the following with jQuery:

$.post('/ControllerName/ActionName', {}, function(result) {
    // handle result here...
});


You can definitely do this just by making another GET or POST to the controller/action you wish to call. In the action, you can determine whether or not the request is coming from Ajax with the extension method Request.IsAjaxRequest(); This will allow your action to respond differently to AJAX requests than it would normally.

For example, I have a TwitterController that reads my twitter RSS feed on the front page. In my Action I do the following:

public ActionResult Feed(string id)
{
    if (Request.IsAjaxRequest())
    {
        // return some pretty Json of my tweets
        return Json(GetMyFeeds(id));
    }
    else
    {
        // I don't want to respond to Non-AJAX requests.
        return RedirectToAction("Index", "Home");
    }
}


You can easily do this using either JQuery or the built-in ASP.NET Ajax libraries. It allows you to return data from the controller or you can actually use partial views.

Take a look at this post I made

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜