在回发mvc后专注于选定的行

时间:2015-07-06 11:04:36

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

您可以在表格中选择一行,然后选择该行,但在我编辑行并保存数据后,它将返回到Index.cshtml,但不再选择该行。我有这个javascript:

$(document).ready(function () {
    var table = $('#example');

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


});

这是观点的一部分:

   <table class="table table-striped table-bordered 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">
                                    <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>

但是,如果我调试javascript它没有达到这个目的:

 $('#example tbody').on('click', 'tr', function () {

这是我的编辑方法;

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

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



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

谢谢

我有这个:

 TempData["editedId"] = id;

并在javascript中:

 alert('@TempData["editedId"]');

但它返回的确切值不是id

我现在在编辑(获取)中:

  Session["id"] = id;

在我的Edit.cshtml中:this:

 $(document).ready(function () {
            var id = '@Session["id"]';
            var element = $("#" + id);
            alert(element);


            $('#example tbody').on('click', 'tr', function () {
                if ($(this).hasClass('dataTable sfs-selectable')) {
                    $(this).removeClass('dataTable sfs-selectable');
                }
                else {
                    element.$('tr.selected').removeClass('dataTable sfs-selectable');
                    $(this).addClass('dataTable sfs-selectable');
                }
            });

        });

我得到了这个ID:var id = '@Session["id"]';

但是现在如何获取所选行?

谢谢

1 个答案:

答案 0 :(得分:2)

编辑页面后,网格页面没有任何关于选择了哪一行的信息。

一个建议可能是在Edit方法中,在一些临时数据中设置Id,如 -

TempData["editedId"] = id;

然后在网格视图页面中,您可以按照建议herehere在jQuery块中检索此值。

然后,您可以使用此值查找表中的特定行并应用所需的样式。

希望有所帮助。