user1618825
user1618825

Reputation:

Ajax.BeginForm Vs Html.BeginForm

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);
            }
        }
    }); 
  1. What is the differnce between Method1 and Method2 as both are using AJAX ?
  2. Which is the most optimal method if a form contains plenty of input elements?
  3. Which is one is considered as standard practice for posting via AJAX?

Any ideas?

Upvotes: 1

Views: 1525

Answers (1)

Sukesh Marla
Sukesh Marla

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

Related Questions