Luke101
Luke101

Reputation: 65248

ASP.NET MVC: Member with the same signature already exists error

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

Upvotes: 2

Views: 1609

Answers (2)

Steve Michelotti
Steve Michelotti

Reputation: 5213

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)

Upvotes: 6

Mark Cidade
Mark Cidade

Reputation: 99957

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()

Upvotes: 1

Related Questions