2

i'm using this code to get a partial view from the server ot put it in a div

    $.ajax(
    {
        type: "POST",
        url: "MyControler/MyAction",
        success: function (result) {
            $('#partialView').html(result);      
        },
        error: function (req, status, error) {
            alert("Coudn't load partial view");
        }
    });

now i would like to do the same thing but i'd call a simple methode returning a string and put the result in a textbox

    $.ajax(
    {
        type: "POST",
        url: "MyControler/MyMethod",
        success: function (result) {
            $('#myTextBox').html(result);      
        },
        error: function (req, status, error) {
            alert("Coudn't load partial view");
        }
    });

the methode would be like that

    public string MyMethod()
    {
        returning "hello";
    }

obviously it doesn't work but is there a way to make it work ?? is there an attribute i should use for the methode

thanks in advance

ps:

from what i red in the first answer i tried this but it doesn't work

    public ActionResult Hello()
    {
        return Content("Hi there!");
    }

is there something wrong with the ajax call ??

$.ajax(
    {
        type: "POST",
         url: "MyControler/Hello",
        success: function (result) {
            $('#myTextBox').html(result);      
     },
    error: function (req, status, error) {
        alert("Coudn't load partial view");
    }
});

ps

I wanted to try on a clean project and it worked fine using a simple method returning a string

since it worked i tried to implement it to my project but it seems it only works from the starting page set up in the global.asax file any idea what i should do to make it work in every pages ???

4 Answers 4

3

You are making an HttpPost call. So make sure your Action method is decorated with HttpPost attribute. Also always try to use Url.Action HTML Helper method for the path to the action method.

The below code should work, assuming you have the Hello Action method present in MMyControlerController.

$.ajax(
        {
         type: "POST",
         url: "@Url.Action("Hello","MyControler")",
         success: function (result) {
             alert("result from server "+result);
             $('#myTextBox').html(result);      
         },
        error: function (req, status, error) {
            alert("Coudn't load partial view");
        }
});

Your action method

[HttpPost]
public ActionResult Hello()
{
  return Content("This is String Content");
}
1

I guess You should use following

$('#myTextBox').attr('value',result); 

instead of

$('#myTextBox').html(result); 
0

You want to return a string from that method, yes, but what you want is an ActionResult that returns the string Check the following question and answer on StackOverflow.

In MVC, how do I return a string result?

3
  • from what i red i tried this but it doesn't work public ActionResult Hello() { return Content("Hi there!"); }
    – Soufiane
    May 27, 2011 at 11:42
  • Please edit your question to when u need to paste code or make large changes May 27, 2011 at 12:02
  • it doesn't help me cause i did just the same thins they said i think there is a probleme with the ajax call could you check please ?
    – Soufiane
    May 27, 2011 at 12:03
0

It's better to use the Url.Action method for cases when you're running under a virtual directory or sub-domain. The Action method will create the correct Url.

Apart from that, it looks like you're POSTing to the site, so have you added the HttpPost attribute to your action?

2
  • could you please explain i'm not sure i understand what you mean
    – Soufiane
    May 30, 2011 at 11:30
  • 1
    The url: "MyController/MyAction" lines, could use the Url.Action method like so url: "@Url.Action("MyAction", "MyController")". If you're using an extension Javascript file, then simple embed/ generate the Url as a variable on the main page, and reference that via the external file.
    – Kieron
    May 30, 2011 at 17:14

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.