我也想调用view1和view2方法。但它不起作用。方法"SubmitMyData"工作正常,并为我的ajax调用返回视图。运行后,我希望执行(例如)view2。在这个例子中,我必须看到视图2
[System.Web.Mvc.Route("Home/SubmitMyData/")]
[System.Web.Http.HttpPost]
public ActionResult SubmitMyData([FromBody]MyParamModel mydata)
{
if (mydata.Prop1.Equals("1"))
{
view1();
return View("view1");
}
else
{
view2();
return View("view2");
}
}
以下是视图的主体方法
public ActionResult view1()
{
ViewBag.Title = "view1";
return View();
}
public ActionResult view2()
{
ViewBag.Title = "view2";
return View();
}
这是我的ajax调用(如果需要查看)
$('#Buttonv').click(function () {
var myData = {Prop1: "10", Prop2: ""};
$.ajax({
type: 'POST',
data: myData,
url: '/Home/SubmitMyData',
})
.success(function (data) {
$('#lblmessage').html(data);
})
.error(function (xhr, ajaxoption, thrownError) {
$('#lblmessage').html("moshkelo" + xhr + "ajaxoption= " + ajaxoption + " throwerror=" + thrownError);
})
//return false;
});
用return RedirectToAction("view2");
替换代码view2();
您需要重定向到一个操作,而不仅仅是像方法一样执行它。
所以你的代码必须喜欢下面的
public ActionResult SubmitMyData([FromBody]MyParamModel mydata)
{
if (mydata.Prop1.Equals("1"))
{
return RedirectToAction("view1");
//return View("view1"); // not required
}
else
{
return RedirectToAction("view2");
// return View("view2"); // not required
}
}