2

I have a controller that inherits from a base controller. Both have an edit (post) action which take two arguments:

On Base controller:

[HttpPost]
public virtual ActionResult Edit(IdType id, FormCollection form)

And in the derived controller:

[HttpPost]
public ActionResult Edit(int id, SomeViewModel viewModel)

If I leave it like this I get an exception because there is an ambiguous call. However, I can't use override on the derived action, because the method signatures don't exactly match. Is there anything I can do here?

UpTheCreek
  • 31,444
  • 34
  • 152
  • 221

3 Answers3

10

As addition to Developer Art's answer a workaround would be:

leave the base method as it is and in your derived class implement the base method and annotate it with [NonAction]

[NonAction]
public override ActionResult Edit(IdType id, FormCollection form)
{
   // do nothing or throw exception
}

[HttpPost]
public ActionResult Edit(int id, SomeViewModel viewModel)
{
   // your implementation
}
Fabiano
  • 5,124
  • 6
  • 42
  • 69
  • Yep, that works. Seems a little hacky ;) so I leave the question open for a while in case there are other ideas - otherwise it'll do the job. Thanks. – UpTheCreek Oct 08 '10 at 12:57
  • 2
    The Attribute is `[NonAction]` not `[NoAction]`, Your solution saved my day – Reza Apr 11 '14 at 12:06
1

I'd chain it:

On Base controller:

[HttpPost]
public virtual ActionResult Edit(IdType id, FormCollection form)

And in the derived controller:

[HttpPost]
public virtual ActionResult Edit(IdType id, FormCollection form)
{
     var newId = //some enum? transform
     var boundModel = UpdateModel(new SomeViewModel(), form);

     return Edit( newId, boundModel );
}

[HttpPost]
public ActionResult Edit(int id, SomeViewModel viewModel)

I haven't tested this, passing a Post method to another Post should work. There could be security implications this way.

John Farrell
  • 24,673
  • 10
  • 77
  • 110
0

That's all what you need to do On Base controller :

adding virtual keyword

enter image description here

On Derived controller :

adding override keyword

enter image description here

Ahmed Sayed
  • 31
  • 1
  • 4