2015-07-06 2 views
0

Я использую asp.net mvc4. И у меня есть сетка, где вы можете выбрать строку, а затем вы можете отредактировать элемент. Например, вы находитесь на странице 3, и вы хотите отредактировать строку на этой странице. Таким образом, вы выбираете эту строку. Но после того, как вы сохраните строку, вы вернетесь на страницу 3, но строка больше не будет выбрана. У меня есть следующее:Оставайтесь на выбранной строке после отправки (сохранить)

Это страница индекса, на которой вы можете выбрать строку. где вы можете выбрать, что вы хотите сделать с выбранной строкой.

<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> 

и это Редактировать страницу:

с двумя кнопками:

<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> 

Спасибо

Я использую это в виде таблицы:

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

И это мой javasc НИИИТ:

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

Это мой пост 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); 
     } 

это Индексный:

<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> 

У меня есть теперь он вот так:

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


}); 

и мое мнение:

<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> 

Но проблема в том, что if ($(this).hasClass('selected')) { является быть не попало

Это было решение:

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

</td> 
+0

из вашего кода я не могу видеть, как вы выборе ваш ряд. Я предполагаю, что есть какой-то код js, который вы не ставите под сомнение. –

+0

Я редактирую сообщение post – InfinityGoesAround

+0

Я редактирую сообщение – InfinityGoesAround

ответ

1

Ну, вы просто должны пройти некоторый параметр для строки через контроллер после редактирования сохранить как IsSelected и на вашем View. И я вижу, что вы делаете это, но по-другому:

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

Так что на ваш взгляд, вы должны сделать это так, я думаю,:

<td class="hidden @((Model.id != null && item.id == Model.id.Value)? "selected" : String.Empty)"> 
    // Your row code 
</td> 
Смежные вопросы