Neil M.
Neil M.

Reputation: 456

Using MVC, How can I pass an anchor tag on a form submit?

In short, I'm trying to hit an anchor on the page I am submitting to.

So my code looks like:

@using (Html.BeginForm("Times#" + Model.SelectedTimeSort, "Patron", FormMethod.Post, new { id = "frmBuildings" }))

but on the form it comes out as:

<form action="/Times%23Evening" id="frmBuildings" method="post">

How do I set this up to submit and pass an anchor tag?

Upvotes: 1

Views: 2761

Answers (2)

cdeutsch
cdeutsch

Reputation: 3952

Just get rid of the Html.BeginForm and use an html form tag with a Url.Action

<form action="@Url.Action("Times", "Patron")#@Model.SelectedTimeSort" id="frmBuildings" method="post">
</form>

Relying on javascript per the correct answer seems like overkill just to keep the "BeginForm" around.

Upvotes: 1

Oli B
Oli B

Reputation: 514

Assuming you are posting to another MVC method on your application:

You would need to add some JavaScript to the page, then in your ViewModel add an Anchor string property which is set in the MVC controller postback code, when the view loads the javascript (onLoad) can detect the parameter and scroll the page to the anchor:

location.hash = "#" + "@(Model.MyAnchorName)"; 

Upvotes: 2

Related Questions