5

Is there a way to add some default parameter to every action in MVC application. So I can get the value of the parameter if send from anchor href.

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
      int currentformcontrolid = 0;
       foreach (var parameter in filterContext.ActionParameters)
       {
           if (parameter.Key == "FormControlID")
           {
               currentformcontrolid = Int32.Parse(parameter.Value.ToString());
           }
       }
}

and my href like this (it's an AJAX anchor call) :

<a 
  data-ajax="true" 
  data-ajax-method="GET" 
  data-ajax-mode="replace" 
  data-ajax-update="#soapfunction-screens" 
  href="/Patient/Intake?FormContorlID=100003"
  data-islandingpage="False"
  id="InkAlcoholDrug" >
    Alcohol Drug Use 
</a>

and my action like (can be any action in the application, every single action should have that parameter FormControlID

public ActionResult WhatEverAction(int TestID=0)
{

}

In OnActionExecuting i can only access TestID but not the parameter of href that is FormControlID

Harsha pps
  • 2,012
  • 2
  • 25
  • 35
danish farhaj
  • 1,316
  • 10
  • 20

3 Answers3

3

You can access the query string values from the Request property of the HttpContext property

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    int currentformcontrolid = Convert.ToInt32(filterContext.HttpContext
       .Request.QueryString["FormControlID"].ToString());
}
  • You are right in way. But i i'm sending ajax call. that anchor is sending the ajax call not a page reload call. – danish farhaj Jun 07 '18 at 09:05
  • There is nothing in your question about an ajax call, but that not should makes difference if its a query string value. –  Jun 07 '18 at 09:07
  • yes you saying right. there is nothing in question as ajax call. forgot to mention :| But i need it that way. I tried your solution with ajax call but not getting the querystring value as it is not the page load call. i think it's not possible – danish farhaj Jun 07 '18 at 09:09
  • 1
    Of course its possible, but we have no idea what your ajax call is. –  Jun 07 '18 at 09:11
  • am updating the question – danish farhaj Jun 07 '18 at 09:11
  • The ajax code you have shown will work fine, except you also have a typo which needs to be corrected - `FormContorlID` is not the same as `FormControlID` –  Jun 07 '18 at 09:53
  • I have tried but can't get the value when ajax call is sent. There is nothing even `Request.Form` has no keys. – danish farhaj Jun 07 '18 at 10:00
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/172670/discussion-between-stephen-muecke-and-danish-farhaj). –  Jun 07 '18 at 10:07
  • You can specify default parameter while specifying route in routeconfig and in defaults you can make it as optional. – Rahul Naik Jun 07 '18 at 10:30
1

You can try specifying that parameter in route:

routes.MapRoute(
    name: “Default”,
    url: “{controller}/{action}/{FormControlID}”,
    defaults: new { controller = “Home”, action = “Index”, FormControlID = UrlParameter.Optional }
);
Rahul Naik
  • 146
  • 1
  • 1
  • 10
1

Try this

protected override void OnActionExecuted(ActionExecutedContext filterContext)
{

    string FormControlID = filterContext.RequestContext.HttpContext.Request["FormControlID"];

    base.OnActionExecuted(filterContext);
}

enter image description here