在MVC4路由中,我有如下路由,
RouteTable.Routes.MapRoute(
name: "Comments_InsertComment",
url: "{controller}/{action}/{parameters}",
defaults: new { controller = "Comments", action = "InsertComment", parameters = UrlParameter.Optional}
);控制器上的方法具有如下签名,
public ActionResult InsertComment(AddParameters parameters) {
//parameters are passed in via jquery ajax post data
}我在ajax中这样调用它们,
$.ajax({
type: 'POST',
url: '/Comments/InsertComment/null',
data:params,
dataType: "json",
error: function (err) {
alert("error - " + err);
},
success: function (data) {
console.log(data);
alert('Your comment was added!');
}
});我遇到的问题是我必须在url中指定/null或/anything,否则就找不到它。当我希望它只是/Comments/InsertComment,然后是post数据。
我已经在这里找到了大多数类似的路由问题,但它们都没有从ajax调用向控制器发布json对象(这是有效的)……
我已尝试将{parameters}设置为{ id },并将id设置为UrlParameter.Optional。我试过把它完全从路线上去掉。我甚至尝试过参数= UrlParameter.Optional。
而且我非常确定它不会与任何其他路由相匹配。
编辑:
AddParameters定义为:
public class AddCommentParameters
{
public string ParentCommentId { get; set; }
public string CommentText { get; set; }
public string BlockReplies { get; set; }
}并且params在javascript中设置为,
var params = { ParentCommentId: null, CommentText: commentText, BlockReplies: null };发布于 2015-01-26 12:56:31
请尝试在ajax中使用此代码块
url: '@Url.Content("~/Comments/InsertComment")',
data: { parameters: params },
dataType: 'JSON'我希望这能有所帮助。
https://stackoverflow.com/questions/28144775
复制相似问题