2

I have two controllers with the same name. One with a [get] and the other with [post]. These two perform totally different functions. Why can't they be the same name?

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult AddCriteriaItem(CriteriaItemAddFormCollection ciafc)
        {
            return View(ciafc);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AddCriteriaItem(CriteriaItemAddFormCollection ciafc)
        {
            string GroupName = (string)Session["SelectedGroupName"];

            //add group or tab
            switch (ciafc.CriteriaID)
            {
                case (int)enums.Criterias.Tab:
                    Template.AddTab(ciafc.TemplateID, ciafc.name, ciafc.description);
                    Response.Redirect(Server.UrlDecode(ciafc.rtn));
                    break;
                case (int)enums.Criterias.Group:
                    Template.AddGroup(ciafc.TemplateID, ciafc.name, ciafc.description, ciafc.TabName);
                    ViewData["CategoryID"] = ciafc.CategoryID;
                    Response.Redirect(Server.UrlDecode(ciafc.rtn));
                    break;
                default:
                    if (!string.IsNullOrEmpty(GroupName.ToString()) && ciafc.CriteriaID > 0 && !string.IsNullOrEmpty(ciafc.TabName))
                    {
                        Template.AddCriteriaItem(ciafc.TabName, GroupName, ciafc.name, ciafc.description, ciafc.options, ciafc.CriteriaID, ciafc.TemplateID);
                    }
                    ViewData["rtn"] = Server.UrlDecode(ciafc.rtn);
                    ViewData["TemplateID"] = ciafc.TemplateID;
                    ViewData["CategoryID"] = ciafc.CategoryID;
                    break;
            }

            Response.Redirect(Server.UrlDecode(ciafc.rtn));
            return View();
        }
Luke101
  • 63,072
  • 85
  • 231
  • 359
  • 1
    Do you really need the GET version? I mean it looks like you are trying to POST a form to a GET method which doesn't really make sense. Do you actually want to have it do `return View(new CriteriaItemAddFormCollection())` and not take a parameter? – Ryan Sep 16 '10 at 18:21
  • Posted a different answer to a similar question here: http://stackoverflow.com/questions/6348372/how-can-i-overload-asp-net-mvc-actions-based-on-the-accepted-http-verbs/10874897#10874897 – The Coder Jun 03 '12 at 23:50

2 Answers2

6

They can't be the same name just because of normal C# compiler rules for overloads with the exact same name and and signature. use the [ActionName] attribute on second overload instead:

[ActionName("AddCriteriaItem")]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveCriteriaItem(CriteriaItemAddFormCollection ciafc)
Steve Michelotti
  • 5,223
  • 1
  • 25
  • 30
1

The error comes from the C# compiler which doesn't take into account attributes for method overloading. Also, custom attributes are opaque to the compiler—it has no way of knowing what they mean.

You're not really adding items in the GET method anyway—it makes more sense to call it something like ViewCriteriaItemAddForm()

Mark Cidade
  • 98,437
  • 31
  • 224
  • 236