我正在使用分页。现在的想法是,如果你离开控制器,将清除分页。我正在使用分页会话。但现在,如果您离开控制器并返回上一个控制器,页面仍然会被选中之前选择的页面。
因此,思想者现在要在辅助类中构建一个泛型方法,以检查控制器名称是否不相同。然后清除会话。并将分页设置为1。
我有这个:
public static object GetSelectedModelId(this HtmlHelper helper,string ControllerName)
{
string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
if ((ControllerName != null) && (currentControllerName.Equals(ControllerName, StringComparison.CurrentCultureIgnoreCase))) {
HttpContext.Current.Session[currentControllerName] = ControllerName;
}
else {
HttpContext.Current.Session.Clear();
return true;
}
return false;
}
public static void SetSelectedModelId(string ControllerName, object ModelId) {
}
但现在如何检查控制器名称是否不相同?
谢谢
我改为:
public static object GetSelectedModelId(string ControllerName)
{
var controller = HttpContext.Current.Request.RequestContext.RouteData.Values["controller"];
string currentControllerName = (string)controller;
if ((ControllerName != null) && (currentControllerName.Equals(ControllerName, StringComparison.CurrentCultureIgnoreCase))) {
return controller;
}
else {
}
}
public static void SetSelectedModelId(string ControllerName, object ModelId)
{
var controller = HttpContext.Current.Request.RequestContext.RouteData.Values["controller"];
string currentControllerName = (string)controller;
if ((ControllerName != null) && (currentControllerName.Equals(ControllerName, StringComparison.CurrentCultureIgnoreCase))) {
HttpContext.Current.Session[currentControllerName] = ControllerName;
}
else {
HttpContext.Current.Session.Clear();
}
}
这就是我的ProductController
的两个编辑方法:
[HttpGet]
public ActionResult Edit(int? id)
{
//TempData["editedId"] = 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);
//Session["IdProduct"] = id;
ModelHelper.GetSelectedModelId("Product");
ViewBag.Model = editModel;
return View(editModel);
}
// POST: /Product/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
[ValidateInput(false)]
public ActionResult Edit(EditProductModel entry)
{
entry.Product.ModificationDate = DateTime.UtcNow;
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);
var page = Session["pageProduct"];
//Session["IdProduct"] = entry.Product.Id;
ModelHelper.GetSelectedModelId("Product");
return RedirectToAction("Index", new { page, id = entry.Product.Id });
}
}
AddDelayedNotification(Resources.Entity.Environment.ItemNotSavedError, Notification.NotificationType.Error);
return Edit(entry.Product.Id);
}
我试试这个:
public static object GetSelectedModelId(string ControllerName)
{
var controller = HttpContext.Current.Request.RequestContext.RouteData.Values["controller"];
string currentControllerName = (string)HttpContext.Current.Session[ControllerName];
if ((controller != null) && (currentControllerName.Equals(ControllerName, StringComparison.CurrentCultureIgnoreCase))) {
return controller;
}
else {
HttpContext.Current.Session.Clear();
}
return controller;
}
但后来我收到了这个错误:
对象引用未设置为对象的实例。
描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。
来源错误:
第49行:字符串currentControllerName =(字符串)HttpContext.Current.Session [ControllerName];
第50行:
第51行:if((controller!= null)&&(currentControllerName.Equals(ControllerName,StringComparison.CurrentCultureIgnoreCase))){
第52行:返回控制器;
第53行:}
如果我这样做:
public static object GetSelectedModelId(string ControllerName)
{
var controller = HttpContext.Current.Request.RequestContext.RouteData.Values["controller"];
string currentControllerName = (string)HttpContext.Current.Session[ControllerName];
if ((controller != null) && (currentControllerName != null) && (currentControllerName.Equals(ControllerName, StringComparison.CurrentCultureIgnoreCase))) {
return controller;
}
else {
HttpContext.Current.Session.Clear();
}
return controller;
}
我仍然没有获得以前的控制器名称
答案 0 :(得分:1)
我这样解决了:
public static object GetSelectedModelId(string ControllerName)
{
//var routeValues = HttpContext.Current.Request.RequestContext.RouteData.Values;
string controller = (string)HttpContext.Current.Session["controller"];
object mid = null;
if (controller != null && controller.Equals(ControllerName, StringComparison.OrdinalIgnoreCase))
mid = HttpContext.Current.Session["SelectedModelId"];
HttpContext.Current.Session.Remove("SelectedModelId");
HttpContext.Current.Session.Remove("controller");
return mid;
}
public static void SetSelectedModelId(string ControllerName, object ModelId)
{
HttpContext.Current.Session["controller"] = ControllerName;
HttpContext.Current.Session["SelectedModelId"] = ModelId;
}
答案 1 :(得分:0)
尝试使用:
var controller = Request.UrlReferrer.Segments
.Skip(1).Take(1).SingleOrDefault() ?? "Home").Trim('/');
// Home is default controller
var action = (Request.UrlReferrer.Segments
.Skip(2).Take(1).SingleOrDefault() ?? "Index").Trim('/');
// Index is default action
这应该让你解压控制器名称,你应该尝试使用TempData
来存储以前的控制器的后续请求。因为HTTP是无状态的。