gpt4 book ai didi

c# - 如果您离开 Controller ,请清除 session

转载 作者:行者123 更新时间:2023-11-30 18:24:23 25 4
gpt4 key购买 nike

我在索引页上有分页。如果您选择一行并编辑该行并保存记录,您将返回到索引,并且之前选择的行和页面可见。这样效果很好 :)。但是,如果您现在转到另一个 View ( Controller )并返回到您更新记录的页面,则分页不是从第 1 页开始,而是从您之前编辑记录的页面开始。那么如何管理您将返回第 1 页。

我有这个:

[Route("sort/{SortColumn}/{SortOrder?}", Name = "Sort-SubmittedForms")]
[Route("sort/{SortColumn}/{SortOrder?}", Name = "Sort-SubmittedForms")]
[Route("page/{Page:int}/{SortColumn}/{SortOrder?}", Name = "Paging-SubmittedForms")]
[Route("search/{SearchString}")]
[Route()]
public ActionResult Index(string searchString, string filter, string currentFilter, string sortColumn, string sortOrder, int? page)
{
IOrderedQueryable<SubmittedForm> entities = db.FilteredSubmittedForms;

if (searchString != null) page = 1; else searchString = currentFilter;
if (page == null && Session["SubmittedFormpage"] != null)
page = (int)Session["SubmittedFormpage"];

bool isHandler = ApplicationUserManager.IsProductHandler(this.User);

bool hideArchivedOrders = true;

if (filter != null) {
int productId = 0;
string stateFilter = null;
if (filter.StartsWith("o_")) { // Order state
if (isHandler)
filter = null;
else {
stateFilter = filter.Substring(2);
OrderState oState = db.OrderStates.FirstOrDefault(s => s.Code == stateFilter);
if (oState == null)
filter = null;
else {
entities = (IOrderedQueryable<SubmittedForm>)entities.Where(
s => s.Order.OrderState.Code == stateFilter
);
AddFixedNotification(String.Format(Resources.Entity.Environment.FitleredByOrderStateMessage, oState.Title));
hideArchivedOrders = false;
}
}
}
else if (filter.StartsWith("s_")) { // Submitted form state
stateFilter = filter.Substring(2);
SubmittedFormStateEnum sfState;
if (SubmittedFormState.TryCodeToId(stateFilter, out sfState)) {
entities = (IOrderedQueryable<SubmittedForm>)entities.Where(
s => s.SubmittedFormStateId == (int) sfState
&& s.Order.OrderState.Code == OrderState.CompletedCode
);
AddFixedNotification(String.Format(Resources.Entity.Environment.FilteredBySubmittedFormStateMessage, SubmittedFormState.IdToLocalizedName(sfState)));
}
else {
filter = null;
}
}
else if (int.TryParse(filter, out productId) && productId > 0) {
Product product = db.Products.Find(productId);
if (product == null) productId = 0;
else {
entities = (IOrderedQueryable<SubmittedForm>)entities.Where(
s => s.Product.Id == productId
);
AddFixedNotification(String.Format(Resources.Entity.Environment.FilteredByProductMessage, product.Name));
}
filter = productId.ToString();
}
else
filter = null;
}

if (isHandler) {
entities = FilterSubmittedFormsForProductHandler(entities);
}
else if (hideArchivedOrders) {
entities = (IOrderedQueryable<SubmittedForm>)entities.Where(
s => s.Order.OrderState.Code != OrderState.ArchivedCode
);
}

if (!String.IsNullOrEmpty(searchString)) {
entities = (IOrderedQueryable<SubmittedForm>)entities.Where(
s => s.Product.Name.ToUpper().Contains(searchString.ToUpper())
);
AddFixedNotification(String.Format(Resources.Entity.Environment.FilteredBySearchTermMessage, searchString));
}

switch (sortColumn) {
case "id":
entities = (sortOrder == "desc") ? entities.OrderByDescending(s => s.Id) : entities.OrderBy(s => s.Id);
break;
case "product":
entities = (sortOrder == "desc") ? entities.OrderByDescending(s => s.Product.Name) : entities.OrderBy(s => s.Product.Name);
break;
case "modified":
entities = (sortOrder == "desc") ? entities.OrderByDescending(s => s.ModificationDate) : entities.OrderBy(s => s.ModificationDate);
break;
case "attach":
entities = (sortOrder == "desc") ? entities.OrderByDescending(s => s.SubmittedFormAttachments.Count) : entities.OrderBy(s => s.SubmittedFormAttachments.Count);
break;
case "orderstate":
entities = (sortOrder == "desc") ? entities.OrderByDescending(s => s.Order.OrderStateId) : entities.OrderBy(s => s.Order.OrderStateId);
break;
default:
sortColumn = "id";
sortOrder = "desc";
entities = (sortOrder == "desc") ? entities.OrderByDescending(s => s.Id) : entities.OrderBy(s => s.Id);
break;
}

ViewBag.SortColumn = sortColumn;
ViewBag.SortOrder = sortOrder == "desc" ? "desc" : "";
ViewBag.SearchString = searchString;
ViewBag.Filter= filter;
Session["SubmittedFormpage"] = page;


if (isHandler)
ViewBag.OrderStates = new Dictionary<string, string>();
else
ViewBag.OrderStates = db.OrderStates.ToDictionary(s => s.Code, s => s.Title);

bool production = !StateHelper.IsTestMode();
ViewBag.ProductsInUse = db.Products.Where(p => p.SubmittedForms.Where(f => f.Order.IsProduction == production).Count() != 0)
.OrderBy(p => p.Name)
.ToDictionary(p => p.Id.ToString(), p => p.Name + " (" + p.SubmittedForms.Where(f => f.Order.IsProduction == production).Count() + ")");

int pageSize = StateHelper.GetPageSize();
int pageNumber = StateHelper.HasPageSizeChanged ? 1 : (page ?? 1);

if (Session["SubmittedFormid"] != null) {
int fid = (int)(Session["SubmittedFormid"]);
//object p = entities.FirstOrDefault(f => f.Id.Equals(fid));
IEnumerable<IEnumerable<SubmittedForm>> pp = entities.Partition(pageSize);
//int calculatedPage = 0;
foreach (var item in pp) {
Debug.Print(pp.Count().ToString());
}
}

return View(entities.ToPagedList(pageNumber, pageSize));
}

谢谢

最佳答案

从一个 View 转到另一个 View 时,您必须将 session 分配给 1。然后下次它会返回到第一页。

session ["SubmittedFormpage"]=1;

关于c# - 如果您离开 Controller ,请清除 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31334345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com