Deblaton Jean-Philippe
Deblaton Jean-Philippe

Reputation: 11388

Overload of actions in the controller

Is it possible to do an overload of the actions in the controller? I haven't found any info about it and when I tried, I got this error:

The current request for action 'Create' on controller type 'InterviewController' is >ambiguous between the following action methods: System.Web.Mvc.ViewResult Create() on type >MvcApplication4.MvcApplication4.InterviewController System.Web.Mvc.ViewResult Create(Int32) on type >MvcApplication4.MvcApplication4.InterviewController

I've tried to do this on another way and I also get a new error that I can't fix. In fact, I created a new action (called create_client instead of create)

I need 2 ways of creating an "opportunite".

  1. I just call the action, and I receive an empty formular in which I just have to insert data.
  2. From a client's page, I must create an "opportunite" with the client that's already completed when the form is displayed to the user. (there is a need of productivity, the user must perform actions as fast as possible).

In the table "opportunite", I've got a column called "FK_opp_client", which is equal to the column "idClient" from the client's table.

I don't get how I can do the second way.

I've created a new action in the controller.

    '
    ' GET: /Opportunite/Create_client

Function Create_client(idclient) As ViewResult
    'Dim FK_Client = (From e In db.client
    'Where(e.idClient = idclient)
    '                    Select e.nomCompteClient).ToString()
    'ViewBag.FK_client = New SelectList(db.client, "idClient", "nomCompteClient", idclient)
    Dim opportunite As opportunite = db.opportunite.Single(Function(o) o.idOpportunite = 5)
    opportunite.FK_Client = idclient
    ViewBag.FK_Client = New SelectList(db.client, "idClient", "nomCompteClient", opportunite.FK_Client)

    Return View(opportunite)
End Function

I've tried a few things to get what I wanted, the last one was to copy what was done in the "Edit" action, but for an empty rank. (so I created an empty rank in my DB). I don't think it was a good idea (imagine someone wants to update the DB where idOpportunite = 5...)

Any better ideas?

Upvotes: 0

Views: 167

Answers (1)

tpeczek
tpeczek

Reputation: 24125

If you want to keep those two methods under the same name, you will have to implement an ActionSelectionAttribute to decorate them, or use them with different verbs (for example POST and PUT). Please read more details on action method selection process here (old but still true).

Different approach might be making your parameter optional and make action to check if it has been passed or not (through nullable type).

Upvotes: 1

Related Questions