admin管理员组文章数量:1403480
I would like to get a form
object and submit the data to server with a button click in Asp MVC.
This is my HTML code:
<form method="post" form-sync="ajax">
@Html.Hidden("InvtId", item.InvtId)
</form>
This is my JS code:
$(document).on("click", "[form-sync='ajax']", function() {
var formdata = new FormData($(this).closest("form")),
url = $(this).data("url");
$.ajax({
url: url,
type: "POST",
data: formdata,
processData: false,
contentType: false,
success: function(response) {
alert(response.message);
return false;
},
});
});
This is my MVC code:
var data = Request["InvtId"];
The problem is the data
variable is empty
Any help would be greatly appreciated, thanks.
I would like to get a form
object and submit the data to server with a button click in Asp MVC.
This is my HTML code:
<form method="post" form-sync="ajax">
@Html.Hidden("InvtId", item.InvtId)
</form>
This is my JS code:
$(document).on("click", "[form-sync='ajax']", function() {
var formdata = new FormData($(this).closest("form")),
url = $(this).data("url");
$.ajax({
url: url,
type: "POST",
data: formdata,
processData: false,
contentType: false,
success: function(response) {
alert(response.message);
return false;
},
});
});
This is my MVC code:
var data = Request["InvtId"];
The problem is the data
variable is empty
Any help would be greatly appreciated, thanks.
Share Improve this question edited Jul 15, 2016 at 15:17 user5870134 asked Jul 15, 2016 at 14:22 denli8denli8 892 silver badges7 bronze badges3 Answers
Reset to default 4- Your
form-sync
attribute is non standard so your HTML is invalid. You should make that adata
attribute. - You need to hook to the
submit
event of the form, notclick
. - The
FormData
constructor expects a DOMElement, not a jQuery object as you are currently passing to it. You can just give thethis
reference as that is the DOMElement. - The
form
has nodata-url
attribute. I assume you want theaction
property instead, which will default to the current page as you haven't provided one explicitly. - The
return
statement in yoursuccess
handler is redundant. - You need to stop the standard form submission (as you're submitting via AJAX instead) by calling
preventDefault()
on the passed submit event.
Here's a plete example with all the above fixes:
<form method="post" data-form-sync="ajax">
@Html.Hidden("InvtId", item.InvtId)
</form>
$(document).on('submit', '[data-form-sync="ajax"]', function(e) {
e.preventDefault();
$.ajax({
url: this.action,
type: 'post',
data: new FormData(this),
processData: false,
contentType: false,
success: function (result) {
alert(result.message);
},
});
})
The problem is that you are passing in a jQuery element and NOT a DOM element.
For the FormData
to actually return what you expect, you need to pass in a DOM element to its constructor.
Here, try this instead:
var formdata = new FormData($(this).closest("form")[0]);
Another problem is that the form has no data-url
attribute.
Use the action property instead, it will return the url of the current page if you have not given a url yourself.
Here, use this instead:
var url = this.action; // or $(this).prop('action');
HTML
< button type="button" class="btn btn-primary"
onclick="saveData()">Save</button>
JS Code Inside of function saveData()
var formData = new FormData();
get values with serializeArray
var formulario = $("#miFormulario").serializeArray();
if there are extra data or files
formulario.push({ "name": fileName, "value": file });
add information to formData
formulario.forEach((d) => {
formData.append(d.name, d.value); });
ajax request
$.ajax({
timeout: 0,
url: "/InfoController/savingInfo",
method: "post",
data: formData,
contentType: false,
processData: false,
success: function (result) { //do something }
});
Controller
[HttpPost] public JsonResult savingInfo() {
if (Request.Files.Count > 0)
{ ... }
var data = Request.Form;
var valor1 = data["dato1"];
return Json(true);
}
本文标签: javascriptHow to get FormData object and submit the form data by ajax use aspnet mvcStack Overflow
版权声明:本文标题:javascript - How to get FormData object and submit the form data by ajax use asp.net mvc - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744365683a2602764.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论