Reputation:
I am trying to submit the form via ajax where i can see two options in using ajax.
Method1
@{
ViewBag.Title = "Index";
var options = new AjaxOptions()
{
Url = Url.Action("Index", "Add"),
LoadingElementId = "saving",
LoadingElementDuration = 2000,
Confirm = "Are you sure you want to submit?"
};
}
@using (Ajax.BeginForm(options))
{
<div id="saving">Loading...</div>
<input type="submit" />
}
Method2
@using (Html.BeginForm(options))
{
<input type="submit" />
}
$.ajax({
type: 'POST',
url: 'Add',
dataType: 'json',
data: { $(form).serialize() },
success: function (data) {
if (data != null) {
console.log(data);
}
}
});
Any ideas?
Upvotes: 1
Views: 1525
Reputation: 177
Ajax.Form makes everything easy or in simple words makes most of the things automatic. Like ModelBinder makes binding between our model input controle easy.
$.ajax gives you complete control over your code. Everything is manual
It completely depend on situation what to choose. If i want to straightforward pass values to controller action i prefer Ajax.BeginForm. When i want to pass something complicated(like parse entire html table and create something and finally pass it) i will prefer $.jQuery
example -
i have a form with some controls like CustomerName,Address,Phone No i prefer mvc ajax
i have a table(grid) which contain many customer records and i want to pass the Json Array i will prefer jQuery
Hope it helped
Upvotes: 1