CORE 2 MVC Ajax 重定向以显示视图
CORE 2 MVC Ajax redirect to display a view
我有一个使用 jQuery 数据表的应用程序,我想单击一行,选择 ID,然后调出一个允许编辑该视图和更新基础数据库的视图.
Ajax 让我进入编辑视图的控制器操作,但我无法显示视图本身。相反,控制器动作只是 returns 到 ajax。我尝试了很多策略,但没有任何乐趣。这是一个基于标准 CORE 模板的简单示例:
@section scripts{
<script>
$(document).on('click', 'button.number', function () {
alert($(this).val());
$.ajax({
method: 'GET',
url: '@Url.Action("About", "Home")'
});
});
</script>
}
<h3>Home Page</h3>
<div>
<button href="#" type="button" class="number" id="one" value="1">1</button>
<button href="#" type="button" class="number" id="two" value="2">2</button>
</div>
运行 调试器显示 About 操作已调用 OK,但未呈现视图 - 它只是 returns 到 ajax。我已经尝试了各种重定向,但任何 "return" 都会返回到 ajax.
是否有解决此问题的方法或者从 JS 到控制器操作的更好方法?谢谢
编辑:
Batuhan 因他的解决方案而受到赞誉,但我重新发布它以清理一些语法并添加我最初目标的参数传递。
$(document).on('click', 'button.number', function () {
var id = $(this).val();
alert(id);
$.ajax
({
method: 'GET',
url: '@Url.Action("About", "Home")',
}).success(function (result) {
window.location.href = '/home/about/' + id;
});
});
这里是“关于”操作的家庭控制器:
public IActionResult About(int id)
{
string parm2 = id.ToString();
ViewBag.msg = parm2;
return View();
}
关于页面:
@{
ViewData["Title"] = "About";
}
<p>
<h1> @ViewBag.msg </h1>
</p>
所有工作都如最初希望的那样!
$(document).on('click', 'button.number', function () {
alert($(this).val());
$.ajax({
method: 'GET',
url: '@Url.Action("About", "Home")'
}).success: function(result){
///this line
window.href='redirect url';
}});;
});
这是一个解决方案,因为您无法从 ajax 调用重定向。它 returns html 形式的视图 所以如果你想回发你需要使用 window.href="url";
我有一个使用 jQuery 数据表的应用程序,我想单击一行,选择 ID,然后调出一个允许编辑该视图和更新基础数据库的视图. Ajax 让我进入编辑视图的控制器操作,但我无法显示视图本身。相反,控制器动作只是 returns 到 ajax。我尝试了很多策略,但没有任何乐趣。这是一个基于标准 CORE 模板的简单示例:
@section scripts{
<script>
$(document).on('click', 'button.number', function () {
alert($(this).val());
$.ajax({
method: 'GET',
url: '@Url.Action("About", "Home")'
});
});
</script>
}
<h3>Home Page</h3>
<div>
<button href="#" type="button" class="number" id="one" value="1">1</button>
<button href="#" type="button" class="number" id="two" value="2">2</button>
</div>
运行 调试器显示 About 操作已调用 OK,但未呈现视图 - 它只是 returns 到 ajax。我已经尝试了各种重定向,但任何 "return" 都会返回到 ajax.
是否有解决此问题的方法或者从 JS 到控制器操作的更好方法?谢谢
编辑: Batuhan 因他的解决方案而受到赞誉,但我重新发布它以清理一些语法并添加我最初目标的参数传递。
$(document).on('click', 'button.number', function () {
var id = $(this).val();
alert(id);
$.ajax
({
method: 'GET',
url: '@Url.Action("About", "Home")',
}).success(function (result) {
window.location.href = '/home/about/' + id;
});
});
这里是“关于”操作的家庭控制器:
public IActionResult About(int id)
{
string parm2 = id.ToString();
ViewBag.msg = parm2;
return View();
}
关于页面:
@{
ViewData["Title"] = "About";
}
<p>
<h1> @ViewBag.msg </h1>
</p>
所有工作都如最初希望的那样!
$(document).on('click', 'button.number', function () {
alert($(this).val());
$.ajax({
method: 'GET',
url: '@Url.Action("About", "Home")'
}).success: function(result){
///this line
window.href='redirect url';
}});;
});
这是一个解决方案,因为您无法从 ajax 调用重定向。它 returns html 形式的视图 所以如果你想回发你需要使用 window.href="url";