1

I am building a mobile site with asp.net mvc3 and just getting to know the mvc framework. I have a requirement that I can't use javascript since some phones are not compatible. I have a form that is being validated on the postback. If validation is invalid I would like to reload the page to the form instead of the top of the page. Is it possible to have my controller tell the view to load to the form id instead of just saying return View("ViewName");?

Thanks for your help, B

bflemi3
  • 6,698
  • 20
  • 88
  • 155

2 Answers2

1

Without javascript your best bet is to use anchors:

<a name="foo"></a>
<form action="@Url.Action(null)#foo" method="post">
    ...
    <input type="submit" value="OK" />
</form>

When you submit the form it will post to the following url /Home/Index#foo which should scroll the browser automatically to the corresponding anchor with this name which is just at the top of the form.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • This gave me an idea. I have two actions (one for post and one for get) for the same page. On the postback if there's an error I redirect back to the same page but append the hash to the end of the url. This forces the get action to fire and load the page with the hash in tact. – bflemi3 Aug 04 '11 at 14:39
0

See this nice solution (there are a lot more but that one is very efficient and small): How do I maintain scroll position in MVC?

Community
  • 1
  • 1
Ofer Zelig
  • 17,068
  • 9
  • 59
  • 93
  • Thanks for the response. I edit my post above; one of my requirements is that i can't use js because not all mobile platforms support js. I do agree though, this solution is very simple but effective. – bflemi3 Aug 04 '11 at 13:56
  • If JS is disabled, I don't think there's a way to do that except maybe using hashes. – Ofer Zelig Aug 04 '11 at 14:01