Reputation: 12302
i had a form in which i want a url of the page from which it got there. Like i am on /Content/Form and i got there from /Content/Document( i want to save this in my database) . what is the best way for this scenario?
Upvotes: 9
Views: 13687
Reputation: 1039558
The best way is to simply pass this information to the controller action.
So for example you could include the request url as a hidden field:
<% using (Html.BeginForm("Process", "SomeController")) { %>
<%= Html.Hidden("requestUrl", Request.RawUrl) %>
<input type="submit" value="OK" />
<% } %>
and inside the corresponding controller action :
[HttpPost]
public ActionResult Process(string requestUrl)
{
// requestUrl will contain the url of the page used to
// render the form
...
}
You could also use the controller and action from route data:
<% using (Html.BeginForm("Process", "SomeController")) { %>
<%= Html.Hidden("controllerName", ViewContext.RouteData.GetRequiredString("controller")) %>
<%= Html.Hidden("actionName", ViewContext.RouteData.GetRequiredString("action")) %>
<input type="submit" value="OK" />
<% } %>
and both controllerName
and actionName
will be sent in the POST request.
Upvotes: 3