且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-10-19 13:47:20

那么,你应该只是传递一些参数为行通过控制器编辑后保存如 IsSelected 并在您查看。我可以看到你这样做,但另一种方式:

  RedirectToAction(指数,新的{页,ID = entry.Product.Id});

所以,在你查看你应该像这样做,我想:

 < TD类=隐藏@((Model.id = NULL&放大器;&安培; item.id == Model.id.Value)!?选择:字符串。空)>
    //你行code
< / TD>

I am using asp.net mvc4. And i have a grid where you can select a row and then you can edit the item. For example you are on page 3 and you want to edit a row on that page. So you select that row. But then after you have save the row you will return back to page 3 but the row is not selected anymore. I have this:

This is the Index page, where you can select a row. where you can choose what you want to do with the selected row.

 <td class="hidden">
     <span>
         @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 })
     </span>
</td>

and this is the Edit page:

with the two buttons:

  <div class="row">
            <div class="col-xs-12 ">
                @Html.RenderNotifications()
            </div>

            <div class="col-xs-12 padding-bottom-10">
                <button type="submit" value="Save" class="btn btn-success"><i class="fa fa-fw fa-check"></i> @Resources.Action.Navigation.Save</button>

                <a href="@Resources.Action.Navigation.JSBack" class="btn btn-danger"><i class="fa fa-fw fa-times"></i>@Resources.Action.Navigation.Cancel </a>
            </div>
        </div>

Thank you

I am using this as table:

 <table class="table table-striped table-bordered table-hover dataTable sfs-selectable sfs-col1-right-aligned">

And this is my 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');
        }
    });


});

This is my Edit method:

 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);
        }

This is my post Edit:

 [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);
        }

this is the Index view:

  <div class="table-responsive" id="example">
            <table class="table table-striped table-bordered table-hover dataTable sfs-selectable sfs-col1-right-aligned" id="example">
                <thead>
                    <tr>
                        <th>
                            @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)
                        </th>
                        <th>
                            @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)
                        </th>
                        <th>
                            @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)
                        </th>
                        <th>
                            @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)
                        </th>
                        <th>
                            @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)
                        </th>
                        <th>
                            @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)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => firstItem.SubmittedForms)
                        </th>
                        <th>
                            @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)
                        </th>
                        <th class="hidden"></th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var item in Model)
                    {
                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => item.Id)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Name)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.IsEnabled)
                            </td>
                            <td>
                                @{
                        bool viewLink = item.IsEnabled;
                        if (!String.IsNullOrEmpty(item.FormName))
                        {
                            var form = item.FormLibraryEntry;
                            if (form == null) {
                                viewLink = false;
                                @Html.DisplayFor(modelItem => item.FormName)
                                <em>(@Resources.Entity.Environment.Removed)</em>
                            }
                            else
                            {
                                @Html.DisplayFor(modelItem => form.Name)
                                <a href="@Url.Action("Details", "FormLibrary", new { id = item.FormName })"><i class="fa fa-fw fa-external-link-square text-info"></i></a>
                            }
                        }
                                }
                            </td>
                            <td>
                                @{
                        if (!String.IsNullOrEmpty(item.TemplateName))
                        {
                            var template = item.TemplateLibraryEntry;
                            if (template == null) {
                                viewLink = false;
                                @Html.DisplayFor(modelItem => item.TemplateName)
                                <em>(@Resources.Entity.Environment.Removed)</em>
                            }
                            else
                            {
                                @Html.DisplayFor(modelItem => template.Name)
                                <a href="@Url.Action("Details", "DesignTemplate", new { id = item.TemplateName })"><i class="fa fa-fw fa-external-link-square text-info"></i></a>
                            }
                        }
                                }
                            </td>
                            <td>
                                @if (!String.IsNullOrEmpty(item.UrlName))
                                {
                                    var defaultProductUri = CustomerConfig.ToHostUri(Request.Url.Scheme, defaultHostHeader, Request.Url.Port, (isProduction ? "" : "TEST/") + item.UrlName);
                                    if (viewLink)
                                    {
                                    @item.UrlName
                                    <a href="@defaultProductUri.ToString()" title="@Resources.Entity.Product.ViewProduct" target="_blank"><i class="fa fa-fw fa-external-link-square text-info"></i></a>
                                    }
                                    else
                                    {
                                    @item.UrlName
                                    }
                                }
                            </td>
                            <td>
                                @{

                        int cnt = item.SubmittedForms.Where(prod => prod.Order.IsProduction == isProduction).Count();
                                    @(cnt.ToString() + " ")
                        if (cnt > 0)
                        {
                                    <a href="@Url.Action("Index", "SubmittedForms", new { filter = item.Id })">
                                        <i class="fa fa-fw fa-external-link-square text-info"></i>
                                    </a>

                        }
                                }
                            </td>
                            <td class="text-nowrap">
                                @item.ModificationDate.ToString("G")
                            </td>
                            <td class="hidden @((Model.id != null && item.Id == Model.id.Value)? "selected" : String.Empty)">
                                <span>


                                    @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 })
                                </span>
                            </td>



                        </tr>
                    }
                </tbody>
            </table>
        </div>

I have it now like this:

$(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');
        }
    });


});

and my view:

 <td class="hidden @((item.Id != 0)? "selected" : String.Empty)">
                                    <span>


                                        @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 })
                                    </span>
                                </td>

But the problem is that if ($(this).hasClass('selected')) { is not be hitted

This was the solution:

  <td class="hidden @(item.Id == (int)(Session["Id"] ?? 0) ? ".dataTable sfs-selectable sfs-selected .table-responsive" : String.Empty) ">

</td>

Well, you should just pass some param for row through controller after edit save like IsSelected and on your View. And i can see you do it but the other way:

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

So on your View you should do it like this, i guess:

<td class="hidden @((Model.id != null && item.id == Model.id.Value)? "selected" : String.Empty)">
    // Your row code
</td>