0

Normally in a normal website/WebApp I would write:

[WebMethod]
public void CommitChange

Then I could call this method from Ajax

$.ajax({ 
    url: "Dummy.aspx/CommitChange"

Now I want to call a method from Ajax code and I am using MVC, so the Url convention is different. And I see that the [WebMethod] annotation is no longer supported. I have the following Ajax:

$.ajax({
                url: '@Url.Action("~/Controller/UploadImage")',
                type: "POST",
                data: {},                   
                dataType: "xml",
                success: function () {
                    alert("Success");
                },
                error: function () {
                    alert("Error");
                }
            });

And the method I want to call:

    [HttpPost]        
    public ActionResult UploadImage(HttpPostedFileBase fileUpload)
    {... }

I am not reaching the method. The problem is surely here:

url: '@Url.Action("~/Controller/UploadImage")'

How can I call the method?

user1960836
  • 1,732
  • 7
  • 27
  • 47
  • 2
    `Url.Action` will only work in a Razor view, not a .js file - see http://stackoverflow.com/questions/9988634/ajax-call-into-mvc-controller-url-issue for options – stuartd Feb 16 '15 at 21:34
  • 2
    .. plus the syntax is `@Url.Action("Action","Controller")` – stuartd Feb 16 '15 at 21:38

0 Answers0