2017-01-26 2 views
0

Ich versuche, Formulare ohne Post zurück über Ajax einzureichen .. mein Code funktioniert nichtWie reiche ich Formular über Ajax ein?

was ist falsch in meinem Skript?

i bin neu mich mit Ajax-Skripte ajax..help ..

unten ist mein Code

Anmerkung: Ich habe zwei Tasten einreichen mit in der Einzelansicht. Ich möchte Ajax-Aufruf machen für beide Aktionen einreichen

meiner Ansicht

@model AjaxEF.Models.Customer 

@using (Html.BeginForm("Index", "Main", FormMethod.Post,new { id="idForm"})) 
{ 
    @Html.EditorForModel() 
    <br /> 
    <input type="submit" name="save" value="Save" /> 
    <input type="submit" name="cancel" value="Cancel" /> 
} 
<script> 
    $("#idForm").submit(function (e) { 
     e.preventDefault(); // avoid to execute the actual submit of the form. 
     var url = "~/Main/Result"; // the script where you handle the form input. 

     $.ajax({ 
      type: "POST", 
      url: url, 
      data: $("#idForm").serialize(), // serializes the form's elements. 
      success: function (data) { 
       alert(data); // show response from the php script. 
      } 
     }); 


    }); 

</script> 

mein Controller

public ActionResult Index() 
     { 
      return View(); 
     } 
     [HttpPost] 
     public ActionResult Index(Customer obj, string save, string cancel) 
     { 
      if (!string.IsNullOrEmpty(save)) 
      { 
       ViewBag.Message = "Customer saved successfully!"; 
      } 
      if (!string.IsNullOrEmpty(cancel)) 
      { 
       ViewBag.Message = "The operation was cancelled!"; 
      } 
      return View("Result", obj); 
     } 
     public ActionResult Result() 
     { 
      return View(); 
     } 

Antwort

0

nicht sicher, warum die andere Antwort gelöscht wurde, aber es war 100% richtig. Die URL, die Sie mit Ihrem AJAX schlagen, ist Ihre Result Aktion, die nur eine Ansicht zurückgibt. Sie müssen Ihre Index Aktion schreiben, und da die Form bereits dort schreiben gesetzt, der beste Weg, diese URL für Ihre AJAX zu bekommen, ist es aus der Form zu wählen:

var url = $('#idForm").attr("action"); 
+0

nicht funktioniert .. .still post zurück passiert – naveen

Verwandte Themen