【发布时间】:2016-07-18 06:36:34
【问题描述】:
我有以下 MVC4 应用程序结构:
我正在尝试从 Index.cshtml 对位于我的 HomeController.cs 中的 SomeActionMethod() 进行 ajax 调用
Index.cshtml 看起来像:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="Scripts/jquery-2.2.2.min.js"></script>
</head>
<body>
<script>
$.ajax({
url: '@Url.Action("SomeActionMethod", "HomeController")',
type: "POST",
cache: false,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function (data) {
alert(data);
},
error: function () {
alert("fail");
}
});
</script>
</body>
</html>
HomeController.cs 看起来像:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace SapRepLanciatoreCAS.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
//trying to invoke from ajax
[HttpPost]
public ActionResult SomeActionMethod()
{
return Json(new { foo = "bar", baz = "Blech" });
}
}
}
路径或控制器有什么问题?
【问题讨论】:
标签: c# asp.net ajax asp.net-mvc asp.net-mvc-4