如何检查是否控制器名称不是MVC中的一样吗? [英] How to check if controller names are not the same in mvc?
问题描述
我使用分页。在ideer现在,如果你离开控制器分页将被清除的。我usinng寻呼会话。但现在如果你离开控制器并回到previous控制器仍处于选中状态的页面之前选择的。
I am using paging. the ideer is now that if you leave the controller paging will be cleared. I am usinng a session for paging. But now if you leave the controller and go back to the previous controller the page is still selected what you selected before.
所以ideer现在是建立在检查,如果控制器名称是不一样的一个辅助类的通用方法。然后清除会话。和寻呼设定为1
So the ideer is now to build a generic method in a helper class that checks if the controller names are not the same. then clear session. and set the paging to 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) {
}
但现在该怎么检查控制器名称是不一样的?
But how to check now if the controller names are not the same?
感谢您
我改成这样:
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的
:
and this for example are my two Edit methods of 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;
}
但后来我得到这个错误:
But then I get this error:
对象引用未设置到对象的实例。
Object reference not set to an instance of an object.
说明:在当前Web请求的执行过程中发生未处理的异常。请查看有关错误的详细信息的堆栈跟踪以及它起源于code。
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.
异常详细信息:System.NullReferenceException:未将对象引用设置到对象的实例
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
源错误:
49号线:字符串currentControllerName =(字符串)HttpContext.Current.Session [ControllerName];结果
第50行:结果
第51行:如果(!(控制器= NULL)及及(currentControllerName.Equals(ControllerName,StringComparison.CurrentCultureIgnoreCase))){结果
第52行:返回控制器;结果
53号线:}
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;
}
我仍然不得到previous控制器名称
I still dont get the previous controller name
推荐答案
我解决这样的:
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;
}
这篇关于如何检查是否控制器名称不是MVC中的一样吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!