MVC 3 Razor - Ajax.BeginForm OnSuccess
Solution 1
Not working is a problem description that's more adapted to people that don't know/care about how computer works and not software developers. Software developers usually describe precisely the problem they are having. They post the exact error message/exception stack trace they are having.
This being said you are asking for alternative solutions, here's mine: don't use any MS Ajax.* helpers
, use jquery directly and unobtrusively, like this
<script type="text/javascript">
$(function () {
$('#confirmation').hide();
$('form').submit(function() {
$('#confirmation').show('slow');
$(this).hide('slow');
return false;
});
});
</script>
@using (Html.BeginForm())
{
<fieldset id="contactForm">
<legend>Message</legend>
<p>
@Html.Label("Email", "Email"):
@Html.TextBox("Email")
</p>
<p>
@Html.Label("Subject", "Subject"):
@Html.TextBox("Subject")
</p>
<p>
@Html.Label("Message", "Message"):
@Html.TextArea("Message")
</p>
<p>
<input type="submit" value="Send" />
</p>
</fieldset>
}
<p id="confirmation">
Thanks!!!
</p>
Notice how the confirmation
paragraph has been externalized from the form.
Solution 2
Are you sure you have all the correct js files referenced in your project? In MVC3, they have moved away from the MS Ajax files. If I remember right - the unobtrusive javascript should be enabled by default, so you should reference the following files: jquery.js, jquery.validate.js, jquery.valudate.unobtrusive.js, jquery.unobtrusive-ajax.js
With these files references - you should be fine.
P.S. There's a very good blog post by Brad Wilson explaining the details of unobtrusive ajax in MVC3, and how it all works. Check it out here: http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-ajax.html
user547043
Updated on April 12, 2020Comments
-
user547043 about 4 years
I'm new to MVC and I'm trying to update my page after I submit my form; but it's not working. I'm only trying to hide the form, and show the contents of a div OnSuccess.
My code is:
<script type="text/javascript"> $(document).ready(function () { $('#confirmation').hide(); }); function MessageConfirmation() { $('#confirmation').show('slow'); $('#contactForm').hide('slow'); } </script> @using (Ajax.BeginForm("Index", new AjaxOptions { OnSuccess = "MessageConfirmation" })) { <fieldset id="contactForm"> <legend>Message</legend> <p> @Html.Label("Email", "Email"): @Html.TextBox("Email") </p> <p> @Html.Label("Subject", "Subject"): @Html.TextBox("Subject") </p> <p> @Html.Label("Message", "Message"): @Html.TextArea("Message") </p> <p> <input type="submit" value="Send" /> </p> </fieldset> <p id="confirmation" onclick="MessageConfirmation()"> Thanks!!! </p> }
Any alternate solutions / ideas are most welcome.
Thanks in advance!
-
user547043 over 13 yearsThanks for your solution. Apologies for the vagueness of the post. I am a Software Developer with a keen interest in Stella Artois. However, the form validation is not firing. How does the client side know if the method is successful?
-
user547043 over 13 yearsThanks Artion, I have now included the js files, and the form is validating, but how does the client side know if the method is successful?
-
Artiom Chilaru over 13 yearsWhat do you mean? That's a pretty vague question =/
-
Artiom Chilaru over 13 yearsIf you have a validateable ajax form, I think usually you would return the form's partial back, so that the validation messages will be shown on the form's result, or a success partial, if the form post was successful. At least that's how I'd do it
-
kyle.stearns almost 11 years@Darin Dimitrov - would you mind providing me with reason(s) to avoid the
MS Ajax.*
helpers? Are your reasons applicable to MVC4 as well? -
Darin Dimitrov almost 11 years@kyle.stearns, you have far greater control of the AJAX request when writing it yourself, and yes, my reasons are applicable in MVC4 as well.
-
kyle.stearns almost 11 years@DarinDimitrov Thanks. I've always felt that the code generated by the helpers is very bloated and would typically rather write it myself. I'm in a situation where I am required to use it but I'd recommend avoiding using them as well.
-
Memet Olsen over 10 years+1 (especially for the first paragraph of your answer)