2

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){....}

2
  • Can you show us your code as to how you've attempted this? You can return content direction to the client but there might be a better solution using partial views and ajax forms. Commented Feb 2, 2010 at 6:20
  • Is your routing really set up to handle this? The URL for the jQuery POST looks a bit odd. You might want to use Html.ActionLink() for generating URLs to action methods, since routing will be taken into account automatically. Also, "returns(renders)" is a non-sequitur, as is "a view page ... into the main.aspx page". You may want to read up on your ASP.NET MVC fundamentals. :) Commented Feb 2, 2010 at 8:25

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

What are you getting back from the call?

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

Comments

0

You can also use:

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

Hope it's what you are looking for : )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.