Consider the following two Models:
BModel, and two partial views:
(bound to AModel)
(bound to BModel)
I have a Controller called
have the following methods:
have the following code:
My partial views look like this:
When I fill in and submit AView I get the following error:
After some research I tried putting
each Index method in
generates the same error (I changed the method names to
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.