ASP.NET MVC - Implicit Form Action
-
05-07-2019 - |
Question
I have a form which is part of a partial view and I need to submit it to a different location based on the context of the view containing the form. I can't use Html.BeginForm because the form has a multipart/form-data enctype so having the action filled in automatically is out the door.
Is there a simple variable I can call to fill in the current controller/action as my form action? I see that ViewContext.RouteData.Values["action"], ViewContext.RouteData.Values["controller"] will return those values, but is there a better accessor?
Thanks!
Solution
The way HtmlHelper.BeginForm()
fills the action and controller automatically is by using Request.RawUrl
. So you could just write:
<form action="<%= Request.RawUrl %>" method="post" enctype="multipart/form-data">
....
</form>
OTHER TIPS
This is what I did
first full view:
<% using (Html.BeginForm("Edit", "Admin",FormMethod.Post,
new { enctype = "multipart/form-data" })){%>
<% Html.RenderPartial("MerchandiseEditDetail", Model); %>
<%}%>>
second full view:
<% using (Html.BeginForm("Create", "Admin",FormMethod.Post,
new { enctype = "multipart/form-data" })){%>
<% Html.RenderPartial("MerchandiseEditDetail", Model); %>
<%}%>
I am still looking for a better solution, but this partially solve my problem for now.