2015-07-10 27 views
0

我正在使用分頁。現在ideer現在如果你離開控制器分頁將被清除。我正在使用分頁會話。但是現在,如果您離開控制器並返回到前一個控制器,該頁面仍將被選中您之前選擇的內容。如何檢查控制器名稱在mvc中是否不相同?

因此,ideer現在要在助手類中構建一個通用方法來檢查控制器名稱是否不相同。然後清除會話。並設置分頁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; 
} 

但後來我得到這個錯誤:如果我做

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 49: string currentControllerName = (string)HttpContext.Current.Session[ControllerName];
Line 50:
Line 51: if ((controller != null) && (currentControllerName.Equals(ControllerName, StringComparison.CurrentCultureIgnoreCase))) {
Line 52: return controller;
Line 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; 
} 

我仍然不明白以前的控制器名稱

回答

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; 
     } 
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是無狀態的。

+0

謝謝你,但請求無法識別 – InfinityGoesAround

+0

我也試過這樣:HttpRequest的。但沒有一個方法UrlReferrer – InfinityGoesAround

+0

嘗試從** Httpcontext **:'HttpContext.Current.Request.UrlReferrer' –

相關問題