Click here to Skip to main content
14,970,019 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two partials inside index.cshtml such as:
@Html.Partial("_EnquiryForm", new SharedModels.SC.Enquiry())
@Html.Partial("_MemberLoginForm", new SharedModels.SC.User())

Does anyone know why the form submissions keep going to the Index actionresult instead of to their own actionresults?

What I have tried:

both partials have form inputs in them, such as:
<form><input type="submit" id="_MemberLoginForm" name="_MemberLoginForm" value="Login" ></form>
and
<form><input type="submit" id="submit" name="_EnquiryForm" value="Submit" ></form>

But both forms always go to
[HttpPost]
public ActionResult Index(Enquiry enquiry)
{}

instead of to what I'd expect:
[HttpPost]
public ActionResult _EnquiryForm(Enquiry enquiry)
{}
or
[HttpPost]
public ActionResult _MemberLoginForm(User user)
{}
Posted
Updated 22-Mar-16 1:24am

The url the data is sent to is governed by the form's "action" attribute, and as you have no action attribute it defaults to the current url, so that will be your Index action. You need to give the form tag in each partial view an action attribute that points to the action you want that form to submit to. You can do this by using the @Url.Action helper inside the form tag;

<form action="@Url.Action( ... )">

or you can use the BeginForm helper to make the whole form tag for you

FormExtensions.BeginForm Method (System.Web.Mvc.Html)[^]
   
v2
Thanks,

I used
<form method="post" action="@Url.Action("_MemberLoginForm", "Home")"></form>
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900