我正在使用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">
这是我的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);
}
这是我的帖子编辑:
[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 :(得分:1)
嗯,您应该在编辑后通过控制器传递一些参数,例如IsSelected
和View
。我可以看到你这样做,但另一方面:
RedirectToAction("Index", new { page, id = entry.Product.Id });
所以在你的View上你应该这样做,我想:
<td class="hidden @((Model.id != null && item.id == Model.id.Value)? "selected" : String.Empty)">
// Your row code
</td>