Reputation: 2655
I have a several question about Ajax forms, i've read some information about, like ajax vx html forms. And the only reason people said was if you want to stay on the same page use ajax else html, to redirect to another page.
Are there other things i should pay attention when using ajax to html.beginform?
Another question, is there a difference between using Ajax.Beginform and then writing a script:
$("form").submit(function () {
$.ajax({
success: function (result) {
$('#result').html(result);
}
});
});
Or without writing a script, i just don't really see the difference, although in a lot of examples i see users do that ... but what is the point then? if you have this:
@using (Ajax.BeginForm("Action", "Controller", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "result" }, new { @class = "form-horizontal", role = "form" }))
{}
Another question i could do basically the same using ajax jquery script and instead of ajax, i could do it with Html.Beginfrom and then on submit i call the function with ajax ...
Another thing i often see when i juse ajax that when a post method is occured in the url i have like = ?length=7 (and i have not idea where this parameter comes from) Anyone idea?
There are so many different approaches but there is nowhere said what is better what is best practice etc ...
Maybe if anyone has a link to some reading about that i would appreciate.
Upvotes: 2
Views: 2362
Reputation: 12815
There is no difference between using @Ajax.BeginForm()
, @Html.BeginForm()
, and just creating a <form>
on your page and using Javascript/jQuery to submit the form via Ajax. As you have heard, use AJAX to allow the browser to save on the page and only refresh part of the DOM instead of loading an entire new page. This often leads to a smoother user experience.
There is no "right" way to do this, it's whatever you're comfortable with. @Ajax.BeginForm()
is just the quickest and easiest way to accomplish this. However, if you come from a Javascript background and want to write all of that code on your own, there's nothing wrong with doing it, other than taking you some more time to write what the AJAX helper method does for you behind the scenes.
To answer your question on "best practices", if you are making an MVC site, then you should use the technologies that are available to you. However, regardless of what you choose, the undeniable best practice is to pick one way of doing things and sticking with it throughout the entire project.
As far as things to read, I prefer books over websites, so I can't help you with quick easy solutions for that. That is also what Google is for.
Upvotes: 3