No action was found on the controller that matches the request

14,182

Try as following: First, create Toy class

public class Toy
{
  public int? toyId {get; set;}
  public string toy {get; set;}
}

Then use it as following...

[System.Web.Mvc.HttpPost]
[System.Web.Mvc.ActionName("Post")]
public ActionResult Post(Toy toy)
{
    // your necessary codes
}

you can take a look here for more information...

Share:
14,182
LbISS
Author by

LbISS

Asp.net developer working in the field of BPMS, trying to acquire PhD in computer science, and working on own mini-startups at spare time.

Updated on June 05, 2022

Comments

  • LbISS
    LbISS almost 2 years

    Sorry for the lame question. I've already read all similar questions and still can't resolve my issue.

    I'm getting 'No action was found on the controller that matches the request' error when calling from ajax:

    $.ajax({
            url: '/api/ToyEdit/Post/',
            dataType: "json",
            type: "POST",
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify({toyId: 1, toy: 'asd'}),
        async: true,
        processData: false,
        cache: false,
        success: function (data) {
            alert(data);
        },
        error: function (xhr) {
            alert(xhr.statusText);
        }
    })
    

    Controller:

    public class ToyEditController : ApiController
    {
        [System.Web.Mvc.HttpGet]
        public EditToyViewModel Get(int? toyId)
        {
            var model = new EditToyViewModel();
    
            if (toyId.HasValue)
            {
                model.Toy = Data.GetToy(toyId.Value);
            }
    
            model.Companies = Data.GetCompanies().ToList();
    
            return model;
        }
    
        [System.Web.Mvc.HttpPost]
        [System.Web.Mvc.ActionName("Post")]
        public ActionResult Post(int? toyId, string toy)
        {
            var a = toy;
            /*new Task<int>(() => DB.Context.Toys.FirstAsync().Id);*/
    
            return new EmptyResult(); 
        }
    }
    

    Routing:

            routes.MapRoute(
                name: "WebApi",
                url: "api/{controller}/{action}/{id}",
                defaults: new { id = UrlParameter.Optional }
            );
    
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
    

    What's wrong with code?

    UPDATE 1:

    OK, it works when i'm using the next code:

    public class Toy
    {
      public int? toyId {get; set;}
      public string toy {get; set;}
    }
    
    [System.Web.Mvc.HttpPost]
    [System.Web.Mvc.ActionName("Post")]
    public ActionResult Post(Toy toy)
    {
        // your necessary codes
    }
    

    But how to pass a few primitive vars?

  • Blue
    Blue almost 9 years
    Again, this doesn't explain why he's getting a 404.
  • PaulShovan
    PaulShovan almost 9 years
    Actually, in web api route.MapRoutes maps by url pattern... for example: "{controller}/{action}/{id}" maps an action of a controller which contains a parameter named id You can take a look here - asp.net/web-api/overview/web-api-routing-and-actions/…
  • LbISS
    LbISS almost 9 years
    It seems when i'm using your code it works. But why i can't just pass two primitive vars?
  • PaulShovan
    PaulShovan almost 9 years
    @LbISS Web API can only bind a single primitive type from the HTTP body. And this is the reason of your confusion