有如下代码:
HomeController.cs
/Home/Index.cshtml
TestController.cs
/Test/Index.cshtml
如果在TextController.cs中的Index函数和/Test/Index.cshtml中添加断点的话,
按下/Home/Index画面中的按钮后,首先会停在TextController.cs中的Index函数,
之后会停在/Test/Index.cshtml中。但是最终结果画面没有迁移到/Test/Index.cshtml中,
依旧停留在/Home/Index.cshtml下,有知道这个问题的解决方案是什么吗?
感谢告知。
index试图中按钮js事件用的ajax请求,浏览器当然不会跳转,要跳转用location.href='/test/index',而不是$.post('/test/index'),$.post是jQuery的ajax发送的post请求。要获取内容需要传递回调来获取test/index返回的内容,比如下面这样
<script>
function onclickFun() {
$.post('/test/index', function (html) {
$('body').append(html);//将ajax请求返回内容添加到body下
});
//要直接跳转到用下面的js代码
//location.href='/test/index'
}
</script>
该回答引用GPTᴼᴾᴱᴺᴬᴵ
这种情况通常是由于Controller中的Index函数没有正确地返回视图导致的。在Controller中的Index函数中,应该调用View方法来返回Index视图,类似于以下代码:
public ActionResult Index()
{
// 执行一些操作
return View();
}
因此,请确保Index函数已正确返回视图,同时确保Index视图中没有出现异常,并且确保在视图中没有执行任何阻止页面跳转的操作。