views:

64

answers:

4

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();
}

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??

A: 

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.

mdresser
A: 

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()); 
              });
Matias
+1  A: 

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');
Darin Dimitrov
+1 for using PartialView
Matias
A: 

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.

Patricia