3

ers! I'm a bit of a noob with this whole MVC thing, and I have run into a problem that I need some help with.

I am currently working on a project for a company that manages foster children, and the main part of this project is an MVC web app for some forms that foster parents must regularly submit to the company. My biggest problem so far has been saving the data that has been entered into the form back to the database. But I recently had a breakthrough when I realized that I could do this:

public ActionResult SaveForm(FormChildTherapeuticWeeklyForm formData)

Which is super awesome, and works perfectly for that first form. But after adding two more forms:

public ActionResult SaveForm(FormChildBasicCareMonthlyProgressReport formData)
public ActionResult SaveForm(FormFosterFamilyRequestForRemovalOrDischarge formData)

I have begun to get a The current request for action 'SaveForm' on controller type 'FormsController' is ambiguous between the following action methods: error.

While I know that I could just name each of the actions differently, I wanted to see if there was a better way to go about this since I ran into this error.

A little more info: each of the three views are strongly-typed to one of the three models/classes.

As I said, I'm still a bit of a noob with the MVC stuff, but I will try my best to provide any other information that you might need.

Thanks!

Daniel Dreier
  • 155
  • 3
  • 10

5 Answers5

1

You could use the ActionNameAttribute to differentiate between them. That way you would do

[ActionName("Child")]
public ActionResult SaveForm(FormChildBasicCareMonthlyProgressReport formData)

And you'd have different routes/use the default route for each. Another way to do it would be to create your own implementation of the ActionFilterAttribute, which would rely on what is passed in, but that's more complicated.

Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
  • Thanks for the suggestion, but I'm not quite sure how that would help. Is there anything else I'd have to add, or is just naming each of them differently enough? (I'm trying that now, btw) – Daniel Dreier Aug 01 '11 at 06:28
  • @Daniel you have to make sure the routes you're using match up with the name changes. – Yuriy Faktorovich Aug 01 '11 at 06:30
  • I think I understand, but it seems like it'll be easier in the long run to have each action named uniquely instead of trying to overload one. Thanks! – Daniel Dreier Aug 01 '11 at 06:37
1

You can, as long as you differentiate them in one way or another. Look at this article for more information: Can you overload controller methods in ASP.NET MVC?

Community
  • 1
  • 1
rkw
  • 7,287
  • 4
  • 26
  • 39
1

Yes, it is better to provide different names for actions, in case they use differen Model classes as argument.

MVC framework uses so called Binders, to bind data that came from web form (or AJAX, or whatever) to CLR object. It is not trivial way to accomplish such binding, since it parse out data from url-encoded-form and tries to map to corresponding CRL object property.

Some object might have same property names, in this case Binder wan't be possible to understand how to make proper mapping;

Alexander Beletsky
  • 19,453
  • 9
  • 63
  • 86
1

You need to use ActionMethodSelectorAttribute in order to disambiguate between calls for those two methods.

See an example here : http://weblogs.asp.net/dfindley/archive/2009/05/31/asp-net-mvc-multiple-buttons-in-the-same-form.aspx

Pop Catalin
  • 61,751
  • 23
  • 87
  • 115
  • While the ActionMethodSelectorAttribute is a little over my head right now, I am really glad you posted that link! I'm going to have a multiple button situation very soon, and that is going to be incredibly helpful! Thanks! – Daniel Dreier Aug 01 '11 at 06:43
0

You can resolve this by using attributes [HttpGet] and [HttpPost]. Applying those attributes will tell what method should be invoked for particular request type.

fedotoves
  • 757
  • 6
  • 7