开发者

mvc controller returns 'html data' instead of view

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


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.


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.


What are you getting back from the call?

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


You can also use:

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

Hope it's what you are looking for : )

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜