保存后返回页面

时间:2015-07-01 14:29:18

标签: c# asp.net asp.net-mvc

我想在保存后返回页面列表中的同一页面。但现在保存后它将返回到第1页而不返回第2页,或3..etc - 该项目是我选择的项目。

我这样试试:

if (SaveDbChanges())
{
    // Record an audit trail event for an updated product.
    {
        ATEvent atEvent = AuditTrailHelper.NewEvent(ATEventType.ProductUpdated, HttpContext, db.Schema, entry.Product);
        atEvent.StringArg = entry.Product.Name;
        ATEventLogger.Current.LogEvent(atEvent);
    }
    AddDelayedNotification(Resources.Entity.Environment.ItemSavedMessage, Notification.NotificationType.Success);
    return RedirectToAction("Index", new { id = entry.Product.Id });

}

所以它必须返回索引视图,但在这种情况下返回第2页。但它可以追溯到索引视图,但接着是第1页。

谢谢

我这样试试:

索引视图:

 <a href="@Resources.Action.Navigation.JSVoid,  new { page = ViewBag.CurrentPage }" class="sfs-actionbutton btn btn-primary disabled"><i class="fa fa-fw fa-edit"></i> @Resources.Action.Navigation.Edit </a>

和索引方法(帖子):

  [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Edit(EditProductModel entry, int page)
        {
            entry.Product.ModificationDate = DateTime.UtcNow;

            if (ModelState.IsValid)
            {
                db.Entry(entry.Product).State = EntityState.Modified;

                db.Entry(entry.Product).Property(model => model.Guid).IsModified = false;
                db.Entry(entry.Product).Property(model => model.CreationDate).IsModified = false;
                db.Entry(entry.Product).Property(model => model.IsProduction).IsModified = false;

                entry.Product.IsProduction = StateHelper.IsTestMode() ? false : true;

                HandleProductSelections(entry.Product);
                SerializeAuthenticationSettings(entry);
                SerializePaymentSettings(entry);
                SerializeConnectors(entry);
                SerializePrefillMappings(entry);

                if (SaveDbChanges()) {
                    // Record an audit trail event for an updated product.
                    {
                        ATEvent atEvent = AuditTrailHelper.NewEvent(ATEventType.ProductUpdated, HttpContext, db.Schema, entry.Product);
                        atEvent.StringArg = entry.Product.Name;
                        ATEventLogger.Current.LogEvent(atEvent);
                    }
                    AddDelayedNotification(Resources.Entity.Environment.ItemSavedMessage, Notification.NotificationType.Success);
                    //return RedirectToAction("Index", new { id = entry.Product.Id });
                    //return RedirectToAction("Index", HttpContext.Request.UrlReferrer.AbsoluteUri);
                    //return Redirect(HttpContext.Request.UrlReferrer.AbsoluteUri);
                   //return Redirect(Request.UrlReferrer.ToString());
                    return RedirectToAction("Index", new { page = page });
                }
            }
            AddDelayedNotification(Resources.Entity.Environment.ItemNotSavedError, Notification.NotificationType.Error);
            return Edit(entry.Product.Id,2,2);
        }

并获得:

 public ActionResult Edit(int? id, int ID, int page)
        {


            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Product product = db.Products.Find(id);
            if (product == null)
            {
                throw new HttpException((int) HttpStatusCode.NotFound, null);
            }

            SetCreateEditProductLists(product, customerSchema);

            EditProductModel editModel = new EditProductModel();
            editModel.Product = product;
            editModel.Db = db;
            //editModel.AuthenticationProviders = AuthenticationProviders.GetProviders(customerSchema);
            //editModel.PaymentProviders = PaymentProviders.GetProviders(customerSchema);
            //editModel.ConnectorProviders = ConnectorProviders.GetProviders(customerSchema);

            DeserializeAuthenticationSettings(editModel);
            DeserializePaymentSettings(editModel);
            DeserializeConnectors(editModel);
            DeserializePrefillMappings(editModel);

            ViewBag.Model = editModel;
            ViewBag.CurrentPage = page;

            return View(editModel);
        }

但如果我这样做:

  <a href="@Url.Action("Edit", new { page = ViewBag.CurrentPage }) " class="btn btn-primary"><i class="fa fa-fw fa-plus"></i> @Resources.Entity.Product.EditProduct</a>

我收到此消息:

The parameters dictionary contains a null entry for parameter 'page' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Edit(System.Nullable`1[System.Int32], Int32, Int32)' in ''. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters

我的编辑现在看起来像这样:

 [HttpGet]
        public ActionResult Edit(int? id, int page)
        {


            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Product product = db.Products.Find(id);
            if (product == null)
            {
                throw new HttpException((int) HttpStatusCode.NotFound, null);
            }

            SetCreateEditProductLists(product, customerSchema);

            EditProductModel editModel = new EditProductModel();
            editModel.Product = product;
            editModel.Db = db;
            //editModel.AuthenticationProviders = AuthenticationProviders.GetProviders(customerSchema);
            //editModel.PaymentProviders = PaymentProviders.GetProviders(customerSchema);
            //editModel.ConnectorProviders = ConnectorProviders.GetProviders(customerSchema);

            DeserializeAuthenticationSettings(editModel);
            DeserializePaymentSettings(editModel);
            DeserializeConnectors(editModel);
            DeserializePrefillMappings(editModel);

            ViewBag.Model = editModel;
            ViewBag.CurrentPage = page;

            return View(editModel);
        }

和我的帖子一样:

 [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Edit(EditProductModel entry, int page)
        {
            entry.Product.ModificationDate = DateTime.UtcNow;

            if (ModelState.IsValid)
            {
                db.Entry(entry.Product).State = EntityState.Modified;

                db.Entry(entry.Product).Property(model => model.Guid).IsModified = false;
                db.Entry(entry.Product).Property(model => model.CreationDate).IsModified = false;
                db.Entry(entry.Product).Property(model => model.IsProduction).IsModified = false;

                entry.Product.IsProduction = StateHelper.IsTestMode() ? false : true;

                HandleProductSelections(entry.Product);
                SerializeAuthenticationSettings(entry);
                SerializePaymentSettings(entry);
                SerializeConnectors(entry);
                SerializePrefillMappings(entry);

                if (SaveDbChanges()) {
                    // Record an audit trail event for an updated product.
                    {
                        ATEvent atEvent = AuditTrailHelper.NewEvent(ATEventType.ProductUpdated, HttpContext, db.Schema, entry.Product);
                        atEvent.StringArg = entry.Product.Name;
                        ATEventLogger.Current.LogEvent(atEvent);
                    }
                    AddDelayedNotification(Resources.Entity.Environment.ItemSavedMessage, Notification.NotificationType.Success);

                    return RedirectToAction("Index", new { page = page });
                }
            }
            AddDelayedNotification(Resources.Entity.Environment.ItemNotSavedError, Notification.NotificationType.Error);
            return Edit(entry.Product.Id,92);
        }

和我的编辑视图一样:

 @using (Html.BeginForm(new { page = ViewBag.CurrentPage })) {
        @Html.AntiForgeryToken()

哦,我解决了,就像这样:

public ActionResult Edit(int?id,int?page)

在get和post方法中。但页面始终为空

问题是如果我这样做:

[HttpGet]        
        public ActionResult Edit(int? id, int? page)
        {
            ViewBag.CurrentPage = 2;

它将始终返回第二页

如果我执行硬编码,为什么它会起作用,如下所示:ViewBag.CurrentPage = 2;

但如果我这样做:ViewBag.CurrentPage = page;那么每次页面为空

谢谢

2 个答案:

答案 0 :(得分:0)

返回第2页(或3,4,5)的最有效方法是在编辑页面中添加一个隐藏变量,其中包含页码。您必须继续将页码传递到索引页之后的每个页面,并在需要时使用它。

你可以做的另一件事就是对你的数据库造成很大的压力,那就是检索所有项目的列表,然后计算你的productID在列表中的位置,然后计算它应该在哪个页面上,然后转到那个页面:

    var ProductIds = db.Products.Select(x=>x.ProductId).ToArray();
    int MyProductIndex = ProductIds.IndexOf(id);
    int itemsperpage = 30;//or whatever the number of items per page
    var pagenum = Math.Ceiling(myproductIndex/itemsperpage);

答案 1 :(得分:0)

我这样解决了:

在索引中我添加了:

   var id = Session["id"];
   Session["id"] = id;