简体   繁体   English

不明确的动作方法调用,由于某种原因ASP.NET MVC 3

[英]Ambiguous action method call, for some reason ASP.NET MVC 3

ers! 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. 对于整个MVC来说,我有点像菜鸟,我遇到了一个需要帮助的问题。

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. 我目前正在为一家管理寄养儿童的公司开展一个项目,该项目的主要部分是一些MVC网络应用程序,用于培养父母必须定期提交给公司的一些表格。 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. 我已经开始得到一个当前的控制器类型'FormsController'上的'SaveForm'请求在以下操作方法之间是不明确的:错误。

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. 正如我所说,我仍然是MVC的一个菜鸟,但我会尽力提供您可能需要的任何其他信息。

Thanks! 谢谢!

You could use the ActionNameAttribute to differentiate between them. 您可以使用ActionNameAttribute来区分它们。 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. 另一种方法是创建自己的ActionFilterAttribute实现,它依赖于传入的内容,但这更复杂。

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? 查看本文以获取更多信息:是否可以重载ASP.NET MVC中的控制器方法?

Yes, it is better to provide different names for actions, in case they use differen Model classes as argument. 是的,如果它们使用不同的Model类作为参数,最好为操作提供不同的名称。

MVC framework uses so called Binders, to bind data that came from web form (or AJAX, or whatever) to CLR object. MVC框架使用所谓的Binders,将来自Web表单(或AJAX或其他)的数据绑定到CLR对象。 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. 实现这种绑定并不是一种简单的方法,因为它从url-encoded-form解析数据并尝试映射到相应的CRL对象属性。

Some object might have same property names, in this case Binder wan't be possible to understand how to make proper mapping; 某些对象可能具有相同的属性名称,在这种情况下,Binder无法理解如何进行正确的映射;

You need to use ActionMethodSelectorAttribute in order to disambiguate between calls for those two methods. 您需要使用ActionMethodSelectorAttribute来消除这两种方法的调用之间的歧义。

See an example here : http://weblogs.asp.net/dfindley/archive/2009/05/31/asp-net-mvc-multiple-buttons-in-the-same-form.aspx 请参阅此处的示例: http//weblogs.asp.net/dfindley/archive/2009/05/31/asp-net-mvc-multiple-buttons-in-the-same-form.aspx

You can resolve this by using attributes [HttpGet] and [HttpPost]. 您可以使用属性[HttpGet]和[HttpPost]来解决此问题。 Applying those attributes will tell what method should be invoked for particular request type. 应用这些属性将告诉应该为特定请求类型调用哪种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM