提交后留在选定行(保存)

我使用的是asp.net mvc4。 我有一个网格,您可以选择一行,然后您可以编辑该项目。 例如,您在第3页,并且您想要编辑该页面上的行。 所以你选择那一行。 但是在保存行之后,您将返回到第3页,但不再选择该行。 我有这个:

这是索引页面,您可以在其中选择一行。 您可以在哪里选择要对所选行执行的操作。

  @if (!String.IsNullOrEmpty(item.UrlName) && !String.IsNullOrEmpty(item.FormName)) { @Html.RouteLink(Resources.Action.Navigation.Preview, "ProductPreview", new { productUrl = item.UrlName, customerSchema = custSchema }, new { target = "_blank" }) } else { @(Resources.Action.Navigation.Preview) } | @Html.ActionLink(Resources.Action.Navigation.Details, "Details", new { id = item.Id }) | @Html.ActionLink(Resources.Action.Navigation.Edit, "Edit", new { id = item.Id })   

这是编辑页面:

用两个按钮:

  
@Html.RenderNotifications()
@Resources.Action.Navigation.Cancel

谢谢

我用它作为表格:

  

这是我的javascript:

 $(document).ready(function () { var table = $('.table-responsive').DataTable(); $('#table-responsive tbody').on('click', 'tr', function () { if ($(this).hasClass('selected')) { $(this).removeClass('selected'); } else { table.$('tr.selected').removeClass('selected'); $(this).addClass('selected'); } }); }); 

这是我的编辑方法:

  public ActionResult Edit(int? id) { var page = Session["page"]; Session["page"] = 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; DeserializeAuthenticationSettings(editModel); DeserializePaymentSettings(editModel); DeserializeConnectors(editModel); DeserializePrefillMappings(editModel); ViewBag.Model = editModel; return View(editModel); } 

这是我的post编辑:

  [HttpPost] [ValidateAntiForgeryToken] [ValidateInput(false)] public ActionResult Edit(EditProductModel entry) { entry.Product.ModificationDate = DateTime.UtcNow; //var page = TempData["page"]; //TempData["page"] = page; //ViewBag.CurrentPage = 2; 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 }); //var page = TempData["page"]; //TempData["page"] = page; var page = Session["page"]; Session["page"] = page; var id = Session["id"]; Session["id"] = id; return RedirectToAction("Index", new { page, id = entry.Product.Id }); } } AddDelayedNotification(Resources.Entity.Environment.ItemNotSavedError, Notification.NotificationType.Error); return Edit(entry.Product.Id); } 

这是索引视图:

  
@foreach (var item in Model) { }
@Html.RouteLink(Html.DisplayNameFor(model => firstItem.Id).ToString(), "Sort-Product", new { sortColumn = "id", sortOrder = (ViewBag.sortColumn == "id" && ViewBag.sortOrder != "desc") ? "desc" : "", searchString = ViewBag.SearchString, filter = ViewBag.Filter }) @ViewHelper.GetSortIndicator("id", ViewBag.sortColumn, ViewBag.sortOrder) @Html.RouteLink(Html.DisplayNameFor(model => firstItem.Name).ToString(), "Sort-Product", new { sortColumn = "name", sortOrder = (ViewBag.sortColumn == "name" && ViewBag.sortOrder != "desc") ? "desc" : "", searchString = ViewBag.SearchString, filter = ViewBag.Filter }) @ViewHelper.GetSortIndicator("name", ViewBag.sortColumn, ViewBag.sortOrder) @Html.RouteLink(Html.DisplayNameFor(model => firstItem.IsEnabled).ToString(), "Sort-Product", new { sortColumn = "enabled", sortOrder = (ViewBag.sortColumn == "enabled" && ViewBag.sortOrder != "desc") ? "desc" : "", searchString = ViewBag.SearchString, filter = ViewBag.Filter }) @ViewHelper.GetSortIndicator("enabled", ViewBag.sortColumn, ViewBag.sortOrder) @Html.RouteLink(Html.DisplayNameFor(model => firstItem.FormName).ToString(), "Sort-Product", new { sortColumn = "formname", sortOrder = (ViewBag.sortColumn == "formname" && ViewBag.sortOrder != "desc") ? "desc" : "", searchString = ViewBag.SearchString, filter = ViewBag.Filter }) @ViewHelper.GetSortIndicator("formname", ViewBag.sortColumn, ViewBag.sortOrder) @Html.RouteLink(Html.DisplayNameFor(model => firstItem.TemplateName).ToString(), "Sort-Product", new { sortColumn = "design", sortOrder = (ViewBag.sortColumn == "design" && ViewBag.sortOrder != "desc") ? "desc" : "", searchString = ViewBag.SearchString, filter = ViewBag.Filter }) @ViewHelper.GetSortIndicator("design", ViewBag.sortColumn, ViewBag.sortOrder) @Html.RouteLink(Resources.Entity.Product.PublicUrl, "Sort-Product", new { sortColumn = "urlname", sortOrder = (ViewBag.sortColumn == "urlname" && ViewBag.sortOrder != "desc") ? "desc" : "", searchString = ViewBag.SearchString, filter = ViewBag.Filter }) @ViewHelper.GetSortIndicator("urlname", ViewBag.sortColumn, ViewBag.sortOrder) @Html.DisplayNameFor(model => firstItem.SubmittedForms) @Html.RouteLink(Html.DisplayNameFor(model => firstItem.ModificationDate).ToString(), "Sort-Product", new { sortColumn = "modified", sortOrder = (ViewBag.sortColumn == "modified" && ViewBag.sortOrder != "desc") ? "desc" : "", searchString = ViewBag.SearchString }) @ViewHelper.GetSortIndicator("modified", ViewBag.sortColumn, ViewBag.sortOrder)
@Html.DisplayFor(modelItem => item.Id) @Html.DisplayFor(modelItem => item.Name) @Html.DisplayFor(modelItem => item.IsEnabled) @{ bool viewLink = item.IsEnabled; if (!String.IsNullOrEmpty(item.FormName)) { var form = item.FormLibraryEntry; if (form == null) { viewLink = false; @Html.DisplayFor(modelItem => item.FormName) (@Resources.Entity.Environment.Removed) } else { @Html.DisplayFor(modelItem => form.Name) } } } @{ if (!String.IsNullOrEmpty(item.TemplateName)) { var template = item.TemplateLibraryEntry; if (template == null) { viewLink = false; @Html.DisplayFor(modelItem => item.TemplateName) (@Resources.Entity.Environment.Removed) } else { @Html.DisplayFor(modelItem => template.Name) } } } @if (!String.IsNullOrEmpty(item.UrlName)) { var defaultProductUri = CustomerConfig.ToHostUri(Request.Url.Scheme, defaultHostHeader, Request.Url.Port, (isProduction ? "" : "TEST/") + item.UrlName); if (viewLink) { @item.UrlName } else { @item.UrlName } } @{ int cnt = item.SubmittedForms.Where(prod => prod.Order.IsProduction == isProduction).Count(); @(cnt.ToString() + " ") if (cnt > 0) { } } @item.ModificationDate.ToString("G") @if (!String.IsNullOrEmpty(item.UrlName) && !String.IsNullOrEmpty(item.FormName)) { @Html.RouteLink(Resources.Action.Navigation.Preview, "ProductPreview", new { productUrl = item.UrlName, customerSchema = custSchema }, new { target = "_blank" }) } else { @(Resources.Action.Navigation.Preview) } | @Html.ActionLink(Resources.Action.Navigation.Details, "Details", new { id = item.Id }) | @Html.ActionLink(Resources.Action.Navigation.Edit, "Edit", new { id = item.Id })

我现在这样:

 $(document).ready(function () { var table = $('#example').data; $('#example tbody').on('click', 'tr', function () { if ($(this).hasClass('selected')) { $(this).removeClass('selected'); } else { table.$('tr.selected').removeClass('selected'); $(this).addClass('selected'); } }); }); 

和我的观点:

    @if (!String.IsNullOrEmpty(item.UrlName) && !String.IsNullOrEmpty(item.FormName)) { @Html.RouteLink(Resources.Action.Navigation.Preview, "ProductPreview", new { productUrl = item.UrlName, customerSchema = custSchema }, new { target = "_blank" }) } else { @(Resources.Action.Navigation.Preview) } | @Html.ActionLink(Resources.Action.Navigation.Details, "Details", new { id = item.Id }) | @Html.ActionLink(Resources.Action.Navigation.Edit, "Edit", new { id = item.Id })   

但问题是if ($(this).hasClass('selected')) {没有被命中

这是解决方案:

    

好吧,你应该在编辑保存之后通过控制器传递一些参数,如IsSelectedView 。 我可以看到你这样做,但另一方面:

 RedirectToAction("Index", new { page, id = entry.Product.Id }); 

所以在你的View上你应该这样做,我想:

  // Your row code