开发者

Same Name For Action Methods With Different Return Types

I've two action methods in my con开发者_JAVA百科troller, 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.


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜