Govind
Govind

Reputation: 979

How to get view name in controller while navigating from view to controller in MVC3

I have below view in my project,

PolicyScreen.cshtml

The above view has below control,

@Html.ActionLink("OK", "AccountScreen", "AccountUC", new { id= ViewBag.id})

Account controller looks like below,

[ActionName("AccountScreen")]
public ActionResult GetPolicyController(int id)
{
if (viewName='PolicyScreen')
{
//do validation
}
}

If I click OK, I am able to hit AccountUC controller and AccountScreen Action Name properly. Now I need to know from which view I was navigated to AccountUC controller?

The answer is PolicyScreen, but I don't know how to get this view name in action method,any help?

Upvotes: 1

Views: 1592

Answers (2)

Shyju
Shyju

Reputation: 218702

Try this

@Html.ActionLink("OK", "AccountScreen", "AccountUC", 
            new { id= ViewBag.id , viewName="replaceCurrentViewNameHere"},null)

Make sure your action method has a parameter to accept the viewname we are sending

[ActionName("AccountScreen")]
public ActionResult GetPolicyController(int id,string viewName)
{
   if (viewName=='PolicyScreen')
   {
     //do validation
   }
}

Upvotes: 1

CodeCaster
CodeCaster

Reputation: 151588

I wonder why you need this, but you can do this by putting the view name in the action link parameters:

new { id = ViewBag.id, fromView = "PolicyScreen" }

And of course you'll need to alter your action method's signature:

public ActionResult AccountScreen(int id, string fromView)

If you want to get the view name automatically rather than hardcode it, see Retrieve the current view name in ASP.NET MVC?.

If you want the action name rather than the view name, see Get current action and controller and use it as a variable in an Html.ActionLink?.

Upvotes: 1

Related Questions