[英]Why can't I redirect to Index?
I cannot return anywhere after setting cookie, the cookie sets properly but after setting the cookie , it wont redirect anywhere .设置cookie后我无法返回任何地方,cookie设置正确但设置cookie后,它不会重定向到任何地方。
Here is my code in view:这是我的代码:
<a onclick="changeGuestCity(@city.LocationId)">
<span class="font-400 opacity-70">@city.LocationTitle</span>
</a>
Here is the JavaScript's function:这是 JavaScript 的 function:
<script>
function changeGuestCity(id) {
$.get("/Home/ChangeGuestCity/" + id);
}
</script>
Here is my the function but doesn't redirect to Index or anywhere else:这是我的 function 但不重定向到索引或其他任何地方:
public IActionResult Index()
{
return View();
}
public IActionResult ChangeGuestCity(int? id)
{
if (id == null)
return Redirect("/");
CookieOptions cityCookie = new CookieOptions()
{
Expires = DateTime.Now.AddDays(30)
};
Response.Cookies.Append("city", id.ToString(), cityCookie);
return Redirect("/");
}
Try this code:试试这个代码:
return Index();
Do you mean you want to redirect to some where when you click the tag?你的意思是当你点击标签时你想重定向到某个地方?
You can't redirect from $.get(), here are two ways:您不能从 $.get() 重定向,这里有两种方法:
You can set the page you want to direct to in href:您可以在 href 中设置要定向到的页面:
<a href="controller/action" onclick="changeGuestCity(@city.LocationId)">
Or redirect in your javascript:或者在您的 javascript 中重定向:
function changeGuestCity(id) {
$.get("/Home/ChangeGuestCity/" + id);
window.location.href = "controller/action";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.