views:

190

answers:

4

i have a module controller which returns(renders) a view page (.aspx) into the main.aspx page

but now i want the controller to return the entire content of .aspx page to the javascript function from where this controller has been called

pls help

my calling function in main.aspx

    $.get('/Module/Select/',
        { TemplateName: TemplateName }, function(result) {
            alert(result);
 });

my controller

 public ActionResult Select(string TemplateName)
    {

return View(TemplateName);           

    }

it should return content of 'TemplateName' to the function( result){....}

+1  A: 

You need to make an asynchronous (ajax) call to the controller's action and pass the object as JSON. In the success callback function just eval the result and you'll get your object.

$("#yourButtonId").click(function(ev) {
  ev.preventDefault();

  $.get('/Module/Select/',
    { TemplateName: TemplateName }, function(result) {
        var myObject = eval('(' + result + ')');
        alert(myObject);
 });
});

In your controller check if the request is ajax request and return the object as JSON.

public ActionResult Select(string TemplateName)
{
    if (Request.IsAjaxRequest())
    {
        return Json(TemplateName);
    }
    return View(TemplateName);           
}

In this way your action will work with ajax and non-ajax requests.

Branislav Abadjimarinov
A: 

What are you getting back from the call?

  $.get('/Module/Select/',
    { TemplateName: TemplateName }, function(result) {
        alert(result); });
SDReyes
A: 

You can also use:

 $.load('/Module/Select/',
    { TemplateName: TemplateName },function(result) {
        alert(result); });
        });

Hope it's what you are looking for : )

God bless,

SD

SDReyes
+1  A: 

I have needed to do a smiliar things. I defined by pages, where I just needed content, as Partial Views.

You Jquery can then GET or POST to a controller action which can return as follows:

return PartialView("ViewName", model);

This can also be strongly typed to a model.

The result var in your JQuery handling function will then contain just the HMTL you need.

RemotecUk