【发布时间】:2014-05-02 09:57:22
【问题描述】:
我在 MVC4 中工作。 我有一个名为“Beauty”的控制器。它有两个动作方法,即“Action1”和“Action2”。 我的视图名称是 Action1。 现在在我看来,我有两个 div。单击第一个 div 时,通过 AJAX 我在控制器中调用“Action1”方法。这工作正常,即我可以点击动作方法“Action1”。 当我单击第二个 div 并尝试通过 AJAX 调用第二个操作方法“Action2”时,我根本无法点击 Action2 方法。
控制器
public class BeautyController : Controller
{
[HttpPost] public JsonResult Action1(string option1)
{
return Json("true");
}
[HttpPost] public JsonResult Action2(string option2)
{
return Json("true");
}
}
Ajax
function Question1(Answers) {
var url = '@Url.Action("Action1", "Beauty")';
var Data = Answers;
if (url != null && url != undefined && Data != null && Data != undefined) {
$.ajax({ url: url,
dataType: "json",
type: "POST",
data: { 'option': Answers },
contentType: 'application/json; charset=utf-8',
success: function (result) {
if (result != null && result != undefined && result == "true")
{
alert("Successfully Inserted!");
}
},
error: function (result) {
alert("error");
}
});
}
}
你能帮我找出问题并解决它吗?
谢谢, 普里亚。
【问题讨论】:
-
请出示一些代码,Priya
-
嗨 Dumisani,我的控制器是 public class BeautyController : Controller { [HttpPost] public JsonResult Action1(string option1) { return Json("true"); } [HttpPost] public JsonResult Action2(string option2) { return Json("true"); } } 和我的 AjAX 在下面的评论中调用
-
函数 Question1(Answers) {var url = '@Url.Action("Action1", "Beauty")'; var Data = Answers;if (url != null && url != undefined && Data != null && Data != undefined) {$.ajax({ url: url, dataType: "json",type: "POST", data : { 'option': Answers }, contentType: 'application/json; charset=utf-8', 成功: function (result) {if (result != null && result != undefined && result == "true") { alert("插入成功!"); } },error: function (result) { alert("error"); } });}}
-
您是说当您将
@Url.Action("Action1", "Beauty")更改为@Url.Action("Action2", "Beauty")时没有任何反应? -
是的杜米萨尼。我试了调试,控制根本不去Action2。
标签: ajax asp.net-mvc-4 controller action