Question

I am using asp.net MVC 4. Is there a way to use two http get methods on an action with the same name but different parameters? Or just a way to have the page be an index page for both so the name of the page is consistent? Also, int cannot be null.

Example:

Used for querystring and searches

   [HttpGet]
    public ActionResult Index(int num, string aString)
   {
   } 

Used for just an action link click(a default search)

 [HttpGet]
  public ActionResult Index()
 {
 } 
Was it helpful?

Solution

No work-around. What you can do is following:

[HttpGet]
public ActionResult Index(int num, string aString)
{
      //if num == null, throw exception or do something
      // if string == null, do something, if not, do something else
} 

OTHER TIPS

 - (Html . BeginForm ( "ViewName", "ControllerName", FormMethod.Get ) {
   Html . Hidden ( "actionname1", "true" ) form elements set 1 submit
   button 1 }

   (Html . BeginForm ( "ViewName", "ControllerName", FormMethod.Get ) {
   Html . Hidden("actionname2", "true") form elements set 2 submit
   button 2 }

   In Controller, check for "true "values for actionname1, actionname2
   and decide on control flow for each. Assumption: Same View and Same
   Controller WITH Two distinct FormMethod.Gets

   ..Paurav

What I actually end up doing was passing in an object with nullable values in one get http request action. As @Display Name suggested above in the comment Microsoft has this functionality built into the .net framework.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top