简体   繁体   English

为什么我不能重定向到索引?

[英]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.

相关问题 为什么我不能得到这个 li 的索引? - Why can't I get the index of this li? 为什么我不能重定向到具有 spring 安全性的页面? - Why can't I redirect to an page with spring security? 为什么我不能在同一个数组中推送索引? - why i can't push index in the same array? 为什么我的 javascript 不能重定向以下代码,我尝试了多种解决方案,我在 StackOverFlow 中找到了 - Why can't my javascript redirect the following code, I've tried multiple solution that i found in StackOverFlow 为什么在SharePoint中关闭模式对话框时无法重定向到另一个URL? - Why I can't redirect to another URL when I close a modal dialog in SharePoint? 我不明白为什么Redirect()无法正常工作 - I don't understand why Redirect() is not working 为什么我不能使用 node.js 重定向到我的失败页面? - Why can't I redirect to my failure page using node.js? 为什么这个索引签名不可选? - Why can't this index signature be optional? 为什么不能使用索引访问此数组? - Why can't you access this array with index? 为什么我不能用这个函数通过javascript设置z-index? (我知道camelcase) - Why can't I set z-index through javascript with this function? (I know about camelcase)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM