【发布时间】: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