Monday 20 February 2012

How to get the current controller and action names in View Mvc3

In MVC we can use the partial views to avoid the redundant code. I have one requirement that the action names are same in two different controllers and there is partial view which has one link which needs to point to the current controller with the same action. I want to access the controller name in View to avoid the redundant code. The solution to get the Controller and Action names in View as follows. Solution:
 @{
        var controller = this.ViewContext.Controller.ValueProvider.GetValue("controller").RawValue.ToString();
    
        Change password
        }
If you want to access the action name
var action=this.ViewContext.Controller.ValueProvider.GetValue("action").RawValue.ToString();

No comments:

Post a Comment