问题描述
我在Asp .net Core中写了一些代码 在最后一行,我有一个按钮,当单击它时,它会通过JQuery定向到Action Method
<button onclick="Go('d6c00401-b95c-443b-8e30-769e6b5f8e03')">Click</button>
// J查询
function Go(Id){
$.get("/Users/Test/"+Id)
}
//动作方法
public IActionResult Test(string Id){return View();}
但是当我单击按钮时,出现此错误
//在FireFox中
未捕获的SyntaxError:标识符在数字文字之后立即开始
//在Google Chrome浏览器中
未捕获到的SyntaxError:无效或意外的令牌
解决方法
有关如何使用Jquery重定向到页面的操作,这是一个有效的演示:
查看:
<button onclick="Go('d6c00401-b95c-443b-8e30-769e6b5f8e03')">Click</button>
@section Scripts
{
<script>
function Go(Id) {
$.get("/Users/Test/" + Id,function () {
window.location.href = "Users/Test";
})
}
</script>
}
控制器:
[HttpGet]
public IActionResult Test(string Id) {
return View();
}
Startup.cs:
public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
{
//...
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");
});
}