Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC - How to get current action from within a partial view?

I have a partial view that is rendered by several views which are returned by several action methods.

The partial view has a form that should post back to the called action method.

It is my understanding that if i just call

<% Html.BeginForm(); %>

from within a view, the form's action attribute will point to the called action method. I can't do this, because I need to set the form's ID attribute for javascript purposes. The overload of Html.BeginForm that will let me set html attributes also requires an explicit controller and action. So, instead of using the Html helper, I could just write the form element out like:

<form action="<%=(NEED TO SOMEHOW GET THE URL TO THE CURRENT ACTION) %>" method="post" id="myForm">

I'm just not sure how to get the URL.

like image 607
Ronnie Overby Avatar asked Sep 18 '09 15:09

Ronnie Overby


2 Answers

Just pass null for the controller and action. The correct values will be substituted. E.g.:

<% using (Html.BeginForm(null, null, FormMethod.Post, new { id = "employeeGroupForm" }))
like image 179
Craig Stuntz Avatar answered Nov 15 '22 06:11

Craig Stuntz


ViewContext.RouteData.Values["action"]

Or something similar gets the job done

like image 30
AndreasN Avatar answered Nov 15 '22 08:11

AndreasN