2
votes

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?

3

3 Answers

9
votes

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
}
1
votes

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.

0
votes

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