14

I Have two method and distinct by http verb:

public class ProductImageController : Controller
{
     [HttpGet]
     public ViewResult Create(int productId)
          {
             return View(productId);
          }

      [HttpPost]
      public ViewResult Create(int productId)
          {
          }
}

but Get Error:

already defines a member called 'Create' with the same parameter types

Mohammadreza
  • 3,139
  • 8
  • 35
  • 56

2 Answers2

28

You can't have multiple methods with the same signature within the same scope like that i.e. same return type and parameter type.

EDIT- Looks like you need to use this: Related question

public class ProductImageController : Controller
{
     [HttpGet]
     public ViewResult Create(int productId)
     {
         return View(productId);
     }

    [HttpPost]
    [ActionName("Create")]
    public ViewResult CreatePost(int productId)
    {
        //return a View() somewhere in here
    }
}
Community
  • 1
  • 1
Chris L
  • 2,262
  • 1
  • 18
  • 33
  • dear @chrise in mvc we can. we can distinct by http verb – Mohammadreza Nov 18 '13 at 08:50
  • 1
    I usually just add an extra variabl to the signature. In most cases I add `, string differentsignature`. That also solves the problem, but is maybe slightly less elegant. – Flater Nov 18 '13 at 09:13
  • This should be the selected answer, I've never seen the `ActionName` attribute before. – keeehlan Sep 03 '15 at 18:14
3

Change the post action method like below:

[HttpPost]
public ViewResult Create(FormCollection formValues)
{
       var productId = formValues["productId"];
}

OR

[HttpPost]
public ViewResult Create(int  productId, FormCollection formValues)
{
 //still using productId, formValues is just an additional parameter 
 //that doesn't need to be implemented.
}
Lin
  • 15,078
  • 4
  • 47
  • 49
  • dear @Lin tnx, but why we can have Index method with same signature and different http verb(post and get) in same controller, but for create i can't?!! – Mohammadreza Nov 19 '13 at 21:08
  • 2
    Mapping incoming browser requests to specified MVC controller actions requires MVC routing engine and HTTP request. By default, the values for action method parameters are retrieved from the request's data collection. First, when incoming browser requests match the routing rule in the "RouteConfig.cs", it'll chose the action method base on the Http method(GET,POST...). But, if you have the same names in Action methods and same types in parameters, only Http method is different, MVC routing engine can't understand what action method to take. – Lin Nov 20 '13 at 02:00
  • I have never seen any Action method include Index method with same signature and different http verb(post and get) that can pass the build without errors. If you have an example, I would like to see it. Then maybe we can figure it out. – Lin Nov 20 '13 at 02:00