Hiding contact form and showing confirmation message
I have an MVC page which has a large amount of content and a 'contact us' form. Once submitted, I simply wish to hide the form and show a confirmation message.
What is the 开发者_运维百科best way to approach this? I've found that all tutorials use a separate 'thank you' controller and view, but this seems counter-intuitive as I would of course have to repeat my page content.
Thank you in advance.
You can use a partial view and Ajax.BeginForm
instead of Html.BeginForm
. This might be what you've read when they mentioned another controller and view. Partial views can perfectly be reused in various containing views.
You can set a variable when the page is submitted and then check that variable when the page posts back. The first time the user enters the page, the value for submitted
will be false, so your submission page will always display first. e.g.
psuedo:
if (!submitted)
// Display submission form
else
// Display thank you form.
Dangerous to do that. Could be that refresh resubmits, it's safer to use PRG - http://en.wikipedia.org/wiki/Post/Redirect/Get. But, to answer the question, if you stay on the same page, use jquery:
$('#myform').submit( function() {
var form = $(this);
$.post(
form.attr('action'),
form.serialize(),
success: function( response ) {
form.hide();
},
failure: function( respones ) {
form.addClass('error');
}
);
return false;
} );
精彩评论