0

I have two methods (Actions) with different option formats but when I run it I'm told they are ambiguous. Methods shown below.

public ActionResult SelectInstitutionToEdit(){...}
public ActionResult SelectInstitutionToEdit(string message){...}
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
physics90
  • 946
  • 3
  • 9
  • 24
  • 2
    you can find you answer here .. [http://stackoverflow.com/questions/436866/can-you-overload-controller-methods-in-asp-net-mvc](http://stackoverflow.com/questions/436866/can-you-overload-controller-methods-in-asp-net-mvc) – mukesh kudi Nov 04 '16 at 19:16

4 Answers4

1

Yes. You cannot have more than one action method with same name (& same http verb).

If you want both to work for Http GET requests, you may keep the second one, and check the value of your message parameter and based on that return the relevant response.

public ActionResult SelectInstitutionToEdit(string message)
{
  if(String.IsNullOrEmpty(message))
  {
    // to do :Return something
  }
  // to do :Return something
}

This will work for yourSite/yourController/SelectInstitutionToEdit and yourSite/yourController/SelectInstitutionToEdit?message=hello

Shyju
  • 214,206
  • 104
  • 411
  • 497
1

Consider changing your signatures:

public ActionResult SelectInstitutionToEdit(){...}  

public ActionResult SelectInstitutionToEditWithString(string message){...}

Will
  • 103
  • 12
1

Although the compiler will run this code without any error because of method overloading but the MVC framework doesn't allow it. But you can use ActionName attribute:

public ActionResult SelectInstitutionToEdit(){...}

[ActionName("SelectInstitutionToEditWithParams")]
public ActionResult SelectInstitutionToEdit(string message){...}

Then you should call it with it's new name. Like this:

@using (Html.BeginForm("SelectInstitutionToEditWithParams", "yourController"))
{

}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
1

Use [ActionName("SomeOtherName")] for the overloaded Action method if its the same get/post call.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197