views:

87

answers:

4

I'm writing an app with ASP.NET MVC where, for various reasons, the navigation is handled through HTML input buttons. What are the Best practices on how to handle this situation?

  1. Set up a Form on the buttons where information needs to be POSTed and just use JavaScript to redirect on the buttons where information doesn't need to be retained

  2. Have all buttons handled through forms, to the point where a mini-form handles navigation on the buttons where the information doesn't need to be retained

    <% using (Html.BeginForm())
       { %>
    <input type="hidden" name="controller" value="1" />
    <input type="hidden" name="action" value="Location" />
    <input id="BackButton" type="submit" value="Go Back" />
    <% } %>
    
  3. Something I haven't thought of here

+1  A: 

3: Style some anchor tags to look like buttons.

On a recent application we implemented option 1. The client liked having "Cancel" buttons on all the forms (...) so we just stuck a click event on the reset inputs which did:

history.go(-1); return false;
Joel Potter
Is that even possible? Often buttons are rendered to look like native buttons on the OS...
Alan Jackson
If you leave the default button style it could be problematic, but I tend to do a bit of custom styling.
Joel Potter
+2  A: 
  1. Javascript is probably your best bet.

Using a mini form seems really heavy handed. You are basically looking to use a button as a link (I'd look seriously at why you are breaking that very basic web convention: buttons post/modify and links navigate) so the more basic you can make it the better.

Alan Jackson
I agree with the "breaking web convention" bit, but ...... what happens if there's no javascript? These days it completely depends on the site and target audience, but it's still a concern. It's one thing to deprive the user of some extra flashy stuff, but depriving the user of being able to navigate is completely different.
James S
+3  A: 

Here you can find a pretty good article on how to style an anchor tag like a button

veggerby
Good link. Also explains nicely why to avoid the default button styles.
Joel Potter
A: 

Use empty <a href="#"> tags and Javascript/jQuery. For example:

In View:

<script src="<%= Url.Content("~/Content/navigation.js") %>" type="text/javascript"></script>
...
<a id="back" href="#">Back</a>
<a id="next" href="#">Next</a>

<script type='text/javascript'>
    var back_href="<%= Url.Action("...") %>";
    var next_href="<%= Url.Action("...") %>";
</script>

Somewhere in navigation.js:

$(document).ready(function() {
    $('a#back').attr('href', back_href);
    $('a#next').attr('href', next_href);
});
eu-ge-ne