1

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?

Govind
  • 979
  • 4
  • 14
  • 45

2 Answers2

1

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?.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • I am calling that action from many view, so only 1 parameter I need to pass, or else I have to change so many logic. Is there any other way to get view name? I have edited the question, kindly look at it. – Govind Aug 21 '14 at 15:58
  • 1
    _Why_ do you need to know from what view a link is clicked? Sounds like a problem that should be solved in another way. Though somewhat unreliable, you can try to [get the referrer URL](http://stackoverflow.com/questions/1471188/how-do-i-get-the-referrer-url-in-an-asp-net-mvc-action). – CodeCaster Aug 21 '14 at 15:59
1

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
   }
}
Shyju
  • 214,206
  • 104
  • 411
  • 497