您好,我是网络开发的新手,并且被困在一个特定的场景中。
我有一个带有两种方法的 map Controller :
public ActionResult Map1(double easting, double northing)
public ActionResult Map2(double easting, double northing)
当被调用时,它们都会使用所需的任何模型导航相应的 View :
return View(model);
然后我有一些 javascript 需要根据传递的操作调用相应的 Controller 方法。
我想将 Controller 方法标记为 [HttpPost],但是当我这样做然后在 javascript 中使用 ajax 请求时,对 View 的调用被吞没并且页面没有被重定向。
目前我让它工作的唯一方法是:
window.location.href = '/Map/' + actionVal + '?easting=' + easting + '&northing=' + northing;
但是使用这个我不能将方法设置为 POST。
有没有人对我应该如何做有更好的想法?
最佳答案
您可以使用此代码:
//Client Side
$.ajax({
type: "POST",
url: '@Url.Action("FirstAjax", "AjaxTest")',
contentType: "application/json; charset=utf-8",
data: {id :1},
dataType: "json",
success: function(result) {
alert(result);
window.locationre=result.url;
}
});
//AjaxTest Controller
[HttpPost]
public ActionResult FirstAjax(string id)
{
return Json(new {url="URL"});
}
关于javascript - MVC如何调用 Controller 的post方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26212987/