如何检查控制器名称是否在mvc中不相同
本文关键字:mvc 是否 何检查 检查 控制器 | 更新日期: 2023-09-27 18:01:55
我正在使用分页。现在的想法是,如果您离开控制器分页将被清除。我正在使用会话进行分页。但是现在,如果你离开控制器回到之前的控制器,页面仍然是你之前选择的。
因此,现在的想法是在helper类中构建一个泛型方法来检查控制器名称是否不相同。然后清除会话。并将分页设置为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请求执行过程中出现未处理的异常。请查看堆栈跟踪以获得有关错误及其在代码中的起源位置的更多信息。
Exception Details: System。NullReferenceException:对象引用没有设置为对象的实例。
源错误:
第49行:string currentControllerName = (string)HttpContext.Current.Session[ControllerName];
50行:
第51行:if ((controller != null) &&(currentControllerName。= (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;
}
我仍然没有得到之前的控制器名
我是这样解决的:
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;
}
尝试使用:
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是无状态的