将序列化表格数据,文件上传路径和其他参数传递给控制器方法 [英] Pass serialize form data , file uploads paths and other parameter to controller method
问题描述
您好,有些人可以帮助我如何传递序列化表单数据,文件上传路径和其他参数,例如
var OfficeHolder_id =123,var AccountDetail_Id =789,var contact_person_id =8910到下面的控制器方法使用ajax post方法。
下面的一些示例代码
查看代码
@using(Html.BeginForm(SaveApplication,CreateApplicationFromSearch,FormMethod.Post,new {enctype =multipart / form-data}))
{
Hi There, can some please help me how to pass serialize form data , file uploads paths and rest of the parameter like
var OfficeHolder_id="123", var AccountDetail_Id="789", var contact_person_id="8910" to the controller method below using ajax post method.
some of the sample code is below
view code
@using (Html.BeginForm("SaveApplication", "CreateApplicationFromSearch", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" id="specific_Item" name="specific_Item" multiple="multiple"/>
<input type="file" id="Position_Description" name="Position_Description" multiple="multiple"/>
<input type="file" id="Employment_Contract" name="Employment_Contract" multiple="multiple"/>
控制器方法
[HttpPost]
public ActionResult SaveApplication(tr_applications Application,
IEnumerable< httppostedfilebase> Employment_Contract,string OfficeHolder_id, int?AccountDetail_Id,string contact_person_id)
我尝试过:
$('#btnSaveClose')。click(function(){
var fileUpload = $(#Resolution_ToApply)。get(0).files;
var files = fileUpload.files;
var fileData = new FormData();
//循环所有文件并将其添加到FormData对象
for(var i = 0; i< files.length; i ++){
fileData.append(files [i] .name,files [i]);
}
$ .ajax({
url:/ CreateApplicationFromSearch / SaveAndCloseApplication,
类型:POST,
contentType:false,//不设置任何内容标题
processData:false,//不处理数据
data:fileData,
成功:函数(响应){
if(响应!= null&& response.success){
}其他{
alert(response.responseText);
}
},
错误:功能(响应){
alert(error!+ response.responseText);}
});
});
controller method
[HttpPost]
public ActionResult SaveApplication(tr_applications Application,
IEnumerable<httppostedfilebase> specific_Item, IEnumerable<httppostedfilebase> Position_Description,
IEnumerable<httppostedfilebase> Employment_Contract,string OfficeHolder_id, int? AccountDetail_Id, string contact_person_id)
What I have tried:
$('#btnSaveClose').click(function () {
var fileUpload = $("#Resolution_ToApply").get(0).files;
var files = fileUpload.files;
var fileData= new FormData();
// Looping over all files and add it to FormData object
for (var i = 0; i < files.length; i++) {
fileData.append(files[i].name, files[i]);
}
$.ajax({
url: "/CreateApplicationFromSearch/SaveAndCloseApplication",
type: "POST",
contentType: false, // Not to set any content header
processData: false, // Not to process data
data: fileData,
success: function (response) {
if (response != null && response.success) {
} else {
alert(response.responseText);
}
},
error: function (response) {
alert("error!" + response.responseText);}
});
});
推荐答案
('#btnSaveClose')。click(function(){
var fileUpload =
('#btnSaveClose').click(function () {
var fileUpload =
(#Resolution_ToApply)。get(0).files;
var files = fileUpload.files;
var fileData = new FormData();
//循环所有文件并将其添加到FormData对象
for(var i = 0; i< ; files.length; i ++){
fileData.append(files [i] .name,files [i]);
}
("#Resolution_ToApply").get(0).files;
var files = fileUpload.files;
var fileData= new FormData();
// Looping over all files and add it to FormData object
for (var i = 0; i < files.length; i++) {
fileData.append(files[i].name, files[i]);
}
.ajax({
url:/ CreateApplicationFromSearch / SaveAndCloseApplication,
类型:POST,
contentType:false,//不设置任何内容标题
processData:false,//不处理数据
data:fileData,
成功:功能(响应){
if(response!= null&& response.success){
}其他{
alert(response.responseText);
}
},
错误:功能(响应){
alert(error!+ response.responseText);}
});
});
.ajax({
url: "/CreateApplicationFromSearch/SaveAndCloseApplication",
type: "POST",
contentType: false, // Not to set any content header
processData: false, // Not to process data
data: fileData,
success: function (response) {
if (response != null && response.success) {
} else {
alert(response.responseText);
}
},
error: function (response) {
alert("error!" + response.responseText);}
});
});
这篇关于将序列化表格数据,文件上传路径和其他参数传递给控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!