获取视图中控制器的动作名

时间:2022-12-01 23:11:50

What is the correct way to get the name of the Action returning the View in MVC3?

获取MVC3中返回视图的操作名称的正确方法是什么?

I am using ViewContext.Controller.ValueProvider.GetValue("action").RawValue to return the name of the Action (Method), which is creating the View in MVC3. I return this in a Partial View, which is included in the View returned by the Action.

我用ViewContext.Controller.ValueProvider.GetValue(“行动”)。RawValue返回操作的名称(方法),它在MVC3中创建视图。我在部分视图中返回这个值,该视图包含在操作返回的视图中。

It works fine for Index, but, when I try to use it for another method name, it always evaluates to false.

它对索引很有效,但是,当我尝试对另一个方法名使用它时,它的值总是为false。

In the immediate window I get the following results:

在当前窗口中,我得到以下结果:

ViewContext.Controller.ValueProvider.GetValue("action").RawValue
"Edit"

ViewContext.Controller.ValueProvider.GetValue("action").RawValue == "Edit"
false

Which is highly confusing, because the first statement evaluates to a string with value "Edit", while comparing this to a string "Edit" returns false?

这非常令人困惑,因为第一个语句的值是“Edit”,而第一个语句的值是“Edit”,而将其与字符串“Edit”的值进行比较会返回false?

Bizarre...

奇怪的……

1 个解决方案

#1


45  

RawValue is an object, so RawValue == "..." calls Object.op_Equality, which comparse by reference rather than by value.

RawValue是一个对象,所以RawValue = "…"调用对象。op_Equality,通过引用而不是值来比较。

Call ViewContext.RouteData.GetRequiredString("action")

调用ViewContext.RouteData.GetRequiredString(“行动”)

#1


45  

RawValue is an object, so RawValue == "..." calls Object.op_Equality, which comparse by reference rather than by value.

RawValue是一个对象,所以RawValue = "…"调用对象。op_Equality,通过引用而不是值来比较。

Call ViewContext.RouteData.GetRequiredString("action")

调用ViewContext.RouteData.GetRequiredString(“行动”)