简体   繁体   中英

Get Controller's Action name in View

What is the correct way to get the name of the Action returning the View in 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.

It works fine for Index, but, when I try to use it for another method name, it always evaluates to 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?

Bizarre...

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

Call ViewContext.RouteData.GetRequiredString("action")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM