0

On my MVC project I have a 'Contact Us' form on the footer of the Layout page so the 'Contact Us' form appears on each page of the website.

I have to check each time the form is submitted, on which page was it, I mean the url of the page that submitted the 'Contact Us' form in its POST method.

For example on the homepage:

http://www.test.com/myWebSite.Site/Home

I need the get: Home

But the problem is that when I submit the form with a POST request, in the Action, Request.Url always gives me that: (The root of the Controller and the Action..)

http://www.test.com/myWebSite.Site/Shared/SubmitContactForm

I think that what I need is the URL of the step before... I am not sure how to do that, any idea?

5
  • Are you wanting to know the url of the page that submitted the 'Contact Us' form in its POST method? Commented Mar 7, 2017 at 22:43
  • You need to fil in the parameters which go to "SubmitContactForm" when you are rendering the page such as home page. And use those parameters to post when post request to SubmitContactForm. Commented Mar 7, 2017 at 23:11
  • @StephenMuecke, Exactly! Commented Mar 8, 2017 at 6:09
  • You could have a parameter in the method (say string returnUrl) and add it in the form - @using(Html.BeginForm( new { returnUrl = Request.RawUrl })) { ... } (or if you did not want it as a query string, then you could assign it to a hidden input that binds to a view model property) Commented Mar 8, 2017 at 6:21
  • Thank you for your help, I didn't want to use is as a query string so used the hidden field idea. Thank you! Commented Mar 8, 2017 at 7:57

1 Answer 1

1

Just a thought (if I understand your question correctly, apologies if not):

In your layout page contact form, add two hidden inputs to pass in and use:

<input type="hidden" name="currentAction" value="@ViewContext.RouteData.Values["Action"].ToString()">

<input type="hidden" id="currentController" value="@ViewContext.RouteData.Values["Controller"].ToString()">

this will give you the exact controller and action served even though your form is located on the shared layout view

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Working!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.