views:

38

answers:

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();
        }
A: 

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
+3  A: 

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