0
私はポストバックなしでフォームを提出しようとしています。私のコードは機能しません。ajax経由でフォームを送信するにはどうすればよいですか?
私のスクリプトで何が間違っていますか?以下
私はAjaxのスクリプトを私にajax..helpする新たなんだ...
は私のコード
ノートです:私は、単一のビューにして、ボタンを2提出しています。私はアクション
私の見解
@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>
私のコントローラ他の回答が削除された理由を
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();
}
は動作しません。..まだポストバックが起こる – naveen