【问题标题】:How to clear session using jquery?如何使用 jquery 清除会话?
【发布时间】:2021-02-19 03:58:59
【问题描述】:

我正在开发 mvc 应用程序。我在视图和控制器中使用了会话。 首先,当我从下拉列表中选择值时。选定的值在视图和控制器中进行管理。 但是当我再次从下拉列表中选择值时,我想清除会话的更改事件。

下面是我的查看代码

@model IEnumerable<StockWatch.DTO.ProductDTO>

@using GridMvc.Html
@using System.Web.UI.WebControls;

@{
    ViewBag.Title = "Index";
    int VendorId = Convert.ToInt32(Session["vendorId"]);
}

<!DOCTYPE html>

<html>
    <head>
         <link href="@Url.Content("~/Content/Custom1.css")" rel="stylesheet" type="text/css" />    
   </head>
<body> 

     @if (Model == null)
     {

    <div id="vendorDropdownDiv" class =" row-fluid Span9" style ="margin-bottom :15px">

                   <div class="span6"  >
                           <div class="span4" style="margin-left:35px;" >
                         <label >Vendor</label>
                    </div>
                    <div class="span6" >
                      @Html.DropDownList("VendorId", ViewData["list"] as SelectList, "-- Select vendor --", new { @id = "vendorDropdown", @name = "VendorId" })

                  </div>
                 </div>                              

                 <div class="span11" style="text-align:right">           
                <input class="btn btn-primary" type="submit" value="Create" id="create"/>
                 <input class="btn btn-default" value="Cancel" style="width:45px;" onclick="window.location.href='@Url.Action("index")    '"/>
           </div>
              </div> 
     }    

  <div id="indexview"></div>

     @if (Model != null)
     {
    <div id="modeldiv" class="span12" style="margin-left:0px;margin-right:0px;">
       <div class="row-fluid" style="margin-top:30px;margin-bottom:10px;">
       <div class="listheading span9" style="font-size:22px;">Products</div>

      <div class="createlink span3" style="text-align:right;margin-left:10px;">
       @Html.ActionLink("+ Add Product", "Create")
    </div>
  }     
</body>
</html>

<script>
    $(document).ready(function () {      
        $('#vendorDropdown').change(function () {                  
        }); 
    });
</script>

控制器代码如下

       public ActionResult Index(int VendorId=0)
    {
        if (VendorId == 0)
        {
            VendorId = Convert.ToInt32(Session["vendorId"]);
        }

        VendorService vendorService = new VendorService();
        SelectList SelectList = new SelectList(vendorService.GetAll().OrderBy(t => t.Name), "Id", "Name", VendorId);
        ViewData["list"] = SelectList;

        var Categorylist = new SelectList(new[] { "Dull", "Anodised", "All" });

        ViewData["Categorylist"] = Categorylist;

        if (VendorId != 0 )
        {
            Session["vendorId"] = VendorId;
            ProductService productService = new ProductService();
            var productlist = new List<ProductDTO>();             
            productlist = productService.GetAll().Where(x => x.VendorId == VendorId).ToList();

            return View(productlist );
        }
        else
        {
            return View();
        }
    }

这里如何使用 jquery 清除 Convert.ToInt32(Session["vendorId"]); 这个会话。 提前谢谢你

【问题讨论】:

  • 创建一个动作 ClearSession 并使用 $.ajax$.get 调用它。
  • 会话在服务器上维护,如果不向服务器发送请求,则无法在客户端上删除。您可以进行 ajax 调用并执行此操作。

标签: jquery asp.net-mvc


【解决方案1】:

会话在服务器上维护,如果不向服务器发送请求,则无法在客户端上删除。

按照 Patrick Hofman 的建议,您可以创建一个动作 ClearSession 并使用 $.ajax 或 $.get 调用它。

【讨论】:

  • 你真的复制并粘贴了@Ondipuli 的评论作为答案吗?
  • @Josh 你可以清楚地看到我在 54 分钟前回答,并且评论是在 52 分钟前添加的
  • 当你不加注意就草草下结论时会发生这种情况。我很抱歉。
【解决方案2】:

由于会话存储在服务器端 - 而不是客户端,您需要指示服务器销毁它。将请求发送到操作方法 可以这样做 -

在您的 mvc 站点中创建一个操作方法 -

public class ControllerName
{
 public ActionResult DestroySession()
 {
     Session = null;
 }
}

然后从页面中,调用以下javascript代码:

<script>

$('#clearSessionButton').click(
function() {
    $.ajax('/ControllerName/DestroySession');
});

</script>

【讨论】:

    【解决方案3】:

    调用 $ajax 或 $post 方法清除会话

         $.post("clearsessionAction",function(data){
            //clear your session 
         });
    

    【讨论】:

      【解决方案4】:

      在您的控制器中创建一个动作,该动作将clearabandon 会话。您可以使用 jQuery ajax 调用该操作,您的会话将被清除。

      【讨论】:

        【解决方案5】:

        我知道可能有点晚了,但我希望这会对你有所帮助 首先,在使用 session 时需要小心。 更好的方法是创建一些将与您的会话一起完成所有工作的类 在你需要创建控制器之后,它将帮助你从 js 中做你想做的所有事情。你会有这样的东西:

        public class SessionManager
        {
            public static int VendorId
            {
                get { return Convert.ToInt32(HttpContext.Current.Session["vendorId"]); }
            }
        
            public static void SetVendorId(int id)
            {
                HttpContext.Current.Session["vendorId"] = id; 
            }
        
            public static void ClearVendorId()
            {
                HttpContext.Current.Session["vendorId"] = null;
            }
        }
        
        public class SessionManagerController : Controller
        {
            [HttpPost]
            public ActionResult GetVendorId()
            {
                return Json(SessionManager.VendorId);
            }
        
            [HttpPost]
            public ActionResult SetVendorId(int id)
            {
                try
                {
                    SessionManager.SetVendorId(id);
                }
                catch (Exception)
                {
                    return Json("false");
                }
                return Json("true");
            }
        
            [HttpPost]
            public void ClearVendorId()
            {
                SessionManager.ClearVendorId();
            }
        }
        

        客户端调用该方法的方式你可以自己选择。

        顺便说一句,在你看来:

         int VendorId = Convert.ToInt32(Session["vendorId"]);
        

        你将使用:

        int VendorId = SessionManager.VendorId
        

        【讨论】:

          【解决方案6】:

          您好,您可以简单地使用sessionStorage.clear();localStorage.removeItem(keyname)sessionStorage.removeItem(keyname)

          【讨论】:

            【解决方案7】:

            您可以将 JQUERY 与 ajax 一起使用,如下所示:

            1 - 在 HTML 文件中使用 id 标签,例如 id="clear_session" 。

            2 - 在你的 js 文件中使用“clear_session”点击事件来启动 ajax:

            var clear_session    = $("#clear_session"); 
            clear_session.click(function(){
                    $.ajax({
                            type: "POST",
                            url: "index.php",
                            data: "do=clear_session",
                            complete: function(data){ 
            
                                    // your desired finishing function
            
                            }           
                    });     
            });
            

            3 - 在 index.php 文件中使用此代码:

            if ($_POST['do'] == 'clear_session')
            {
                // your desired function
            }
            

            请回复以获取更多指南。 赛义德·罗斯塔米。 I.R.I 的 IT 工程师 +98 935 516 90 58

            【讨论】:

              猜你喜欢
              • 2014-09-15
              • 1970-01-01
              • 2016-10-29
              • 1970-01-01
              • 2016-05-19
              • 2020-09-27
              • 2015-03-01
              • 2013-12-06
              • 2012-04-22
              相关资源
              最近更新 更多