[RESOLVED]Asp.net MVC Controller / Model Quandry

Consider the following two Models: AModel
(Class A)
 and BModel
(Class B)
, and two partial views:AView
(bound to AModel)
 and BView
(bound to BModel)
.

I have a Controller called TestController.
In TestController I
have the following methods:

<HttpPost>
Function Index(model as AModel) As ActionResult
    Return View(model)
End Function

<HttpPost>
Function Index(model as BModel) As ActionResult
    Return View(model)
End Function

In Index I
have the following code:

@Using Html.BeginForm()
@<div>
    @Html.Partial("~/Views/Test/AView.vbhtml")  ' this is here for test only
    <div><input type="submit" name="submit" value="Submit" /></div>
</div>
End Using

My partial views look like this:

@ModelType Company.Domain.Models.ModelA '(or ModelB for the other)

<div>@Html.LabelFor(Function(m) m.Name)</div>
<div>@Html.TextBoxFor(Function(m) m.Name)</div>

... and so on, several Input fields follow ...

When I fill in and submit AView I get the following error:

The current request for action 'Index' on controller type 'TestController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Index(Company.Domain.Models.AModel) on type Company.WebUI.Controllers.TestController
System.Web.Mvc.ActionResult Index(Company.Domain.Models.BModel) on type Company.WebUI.Controllers.TestController

After some research I tried putting <ActionName("Index")> over
each Index method in TestController but
generates the same error (I changed the method names to AIndex and BIndexthen
placed the action name attribute above them and tested
.)

What I’m trying to achieve: I have two forms I need my user to fill out (one after the other). Each form has it’s own set of properties and validation (naturally), hence AModel and BModel. I created two matching Partial Views (partial because I
will want to use those views within other main views later on).

I’m lost as to how to achieve what I want here because ASP.NET isn’t recognizing the different method signatures, nor am I sure I’m going about doing this correctly under ASP.NET MVC anyway. Under a competing platform I would simply #INCLUDE my
pages as needed.

I hope my explanation is clear, appreciate any advice.

put the names of the 2 actions as IndexA and IndexB

Html.BeginForm supports adding action name

ASP.NET MVC Controllers do not support overloading Controller Actions (e.g. you cannot have two Actions with the same name that expect the same type of request like POST).

Using an ActionName attribute

You really have two options here. You could explitly change the name of each of your actions :

<HttpPost>
Function A(model as AModel) As ActionResult
    Return View("Index",model)
End Function

<HttpPost>
Function B(model as BModel) As ActionResult
    Return View("Index",model)
End Function

Or you can explicitly set a name for the action using an [ActionName] attribute :

<HttpPost>
<ActionName("A")>
Function Index(model as AModel) As ActionResult
    Return View("Index",model)
End Function

<HttpPost>
<ActionName("B")>
Function Index(model as BModel) As ActionResult
    Return View("Index",model)
End Function

You obviously don’t need to assign an ActionName to both of them, but at least one will need it in order differentiate between the two.

Ensuring Proper Posting

Another issue here is that ASP.NET MVC is not going to know which particular Action that you are trying to POST to from your form unless you explicitly specify it within the BeginForm() Helper method :

@Using Html.BeginForm("A","Test")
@<div>
    @Html.Partial("~/Views/Test/AView.vbhtml")  ' this is here for test only
    <div><input type="submit" name="submit" value="Submit" /></div>
</div>
End Using

If you use the ActionName attributes mentioned above (or simply rename the Action), this should now refer to the "A" method and will POST your "A" Model to the appropriate action. If you are going to be setting some of these values dynamically (such
as an AJAX call
), then you’ll likely want to include the <form> itself within the Partial Views to avoid any confusion of where to POST to.

Leave a Reply