开发者

AJAX with Jquery in ASP.MVC2

I'd like to get something via Ajax and put it into

What i have to return from Controler Action to put this into that DIV? Right now i had standard ASP.MVC view as result...

public ActrionResult MyAction() {
   return View();
开发者_如何学Go}

and in javascript i have:

$.get(<url to controler action>, null, function(data) { $(#result).html(data); })

Typical action associated with MyAction, with reload page works perfect. but when i do $.get(...) i got whole page in "data" (content and master page). How can i get only part of View(), without MsterPage??


You could return a partial:

public ActrionResult MyAction() 
{
   return PartialView();
}

Where you have MyAction.ascx partial containing some HTML fragment:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<div>Hello</div>

And you inject this partial using an AJAX call into the #result div:

$('#result').load('/home/myaction');


You'll want to use

public ActrionResult MyAction() {
    var stuff = new { id = 1, name = "test" }; //or whatever stuff you need in your page
    return Json(stuff);
}

from your ActionResult method.


As an alternative to a json response, you can return the html. All you need to do is to filter and get the html that you want to append from the 'data' variable.

$.get(<url to controler action>, null, function(data) { 
              $('#result').html($(data).find('#partOfViewID').html()); 
              });


that's not a problem with jquery / ajax. you need an action that returns a partial view. not one that returns a view with a master page.

you can use this part:

$.get(, null, function(data) { $(#result).html(data); })

but your action will have to have a partial view associated with it, not a regular view with a master page.

so make a new action. add a view to it, check the partial view checkbox. and put whatever you want to be reloaded in that partial view.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜