Coder
Coder

Reputation: 35

Same Name For Action Methods With Different Return Types

I've two action methods in my controller, both are called on POST request but have different return type:

public JsonResult AJAXCreate()

public string AJAXCreateNSave([Bind(Exclude = "Id, OrderItems")]Order order)

When I rename the second one to AJAXCreate, it is not called at all. I want to use same name for both action methods.

Upvotes: 2

Views: 11453

Answers (2)

Ed Chapel
Ed Chapel

Reputation: 6932

Overloaded methods are not allowed in ASP.NET MVC without an attribute to specify a different action name. Check out this similar question and answer: Can you overload controller methods in ASP.NET MVC?

Upvotes: 4

Matthew Abbott
Matthew Abbott

Reputation: 61599

Much like operations in WCF services, no two actions can have the same name, unless they target different HTTP verbs, e.g.:

public ActionResult MyAction() { }

[HttpPost]
public ActionResult MyAction(MyModel model) { }

If you try and use two actions with the same name, MVC doesn't know which action to select.

Upvotes: 5

Related Questions