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调用时,我得到“在匹配请求的控制器上没有发现任何操作”的错误:
$.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:
更新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?
但是如何通过一些原始的vars呢?
1 个解决方案
#1
2
Try as following: First, create Toy
class
尝试如下:首先,创建Toy类
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...
你可以在这里查看更多信息……
#1
2
Try as following: First, create Toy
class
尝试如下:首先,创建Toy类
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...
你可以在这里查看更多信息……