【问题标题】:Change boolean to false via button onclick method located in navigation bar通过位于导航栏中的按钮 onclick 方法将布尔值更改为 false
【发布时间】:2022-11-28 12:56:56
【问题描述】:

我想使用共享导航栏中按钮的 onclick 方法将以下变量更改为 false。

多变的

public static bool LoginStatus { get; set; } = true;

到目前为止我所拥有的不起作用:

按钮

<button type="button" class="btn btn-primary" onclick=@Apex_Leaderboard_Website.Models.LoginViewModel.LoginStatus = false>Log Out</button>

我已经尝试过一种形式,但是使用共享导航栏中的按钮很难将其提交给适当的处理程序。

【问题讨论】:

    标签: c# html asp.net asp.net-core model-view-controller


    【解决方案1】:

    你应该使用 asp:Button 因为你想修改 C# 类属性。然后你可以在它上面发生 OnClick 事件,这样你就可以调用后端(代码隐藏)方法,在那个方法中,你可以将 enter code here 设置为 false

    Here 是你的参考。

    【讨论】:

      【解决方案2】:

      您可以使用发布表单或使用 js。

      <form method="post" asp-route-LoginStatus=false>
          <input type="submit" value="Log Out" />
      </form>
      

      js:

      <button type="button" class="btn btn-primary" onclick="passData()">Log Out</button>
      
      function passData() {
                  $.ajax({
                      type: "POST",
                      url: "xxx",
                      data: { LoginStatus: false },
                      success: function(data) {
                         
                      }
                  });
      
              }
      

      行动:

      public ActionResult xxx(bool LoginStatus)
      {
          ....
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-27
        • 1970-01-01
        • 1970-01-01
        • 2018-05-03
        相关资源
        最近更新 更多